2012-01-18 153 views
0

嗨,我创建了一个网站,我有一个OpenId登录。 当按下登录按钮我打电话,基本上有一个AJAX调用把这称为AJAX方法:DotNetOpenAuth和ajax

[WebMethod] 
public static LoginResult Login(string url) 
{ 
    Identifier id; 
    LoginResult result = new LoginResult(); 
    if (Identifier.TryParse(url, out id)) 
    { 
     try 
     { 
      //request openid_identifier 
      FetchRequest fetch = new FetchRequest(); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Name.Alias); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Name.First); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last); 
      string rootUrl = "http://" + HttpContext.Current.Request.Headers["Host"] + "/"; 
      IAuthenticationRequest request = openid.CreateRequest(url, new Realm(rootUrl), new Uri(rootUrl + "?action=verify")); 
      request.AddExtension(fetch); 
      result.RedirectUrl = request.RedirectingResponse.Headers["Location"]; 
     } 
     catch (ProtocolException ex) 
     { 
      result.ErrorMessage = ex.Message; 
     } 
    } 
    else 
    { 
     result.ErrorMessage = "Could not parse identifier!"; 
    } 

    return result; 
} 

这个伟大工程的JavaScript得到“的redirectUrl”,并重定向到它,在开放ID验证后供应商做我送回到一些这样的事

http://localhost:33386/?action=verify&dnoa.userSuppliedIdentifier=https%3A%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid&openid.ns=http%3A%2F%2Fspecs.openid.net%2 ............

但是,当我打电话openid.GetResponse()并检查状态其失败。 如果我检查异常其包含以下消息

的openid.return_to参数 (HTTP://本地主机:33386 /动作=验证& dnoa.userSuppliedIdentifier = HTTPS:%2F%2Fwww.google。 (http:// localhost:33386/default.aspx?action = verify & dnoa.userSuppliedIdentifier = https:%2F%2Fwww.google.com%2Faccounts% 与实际的网址不符 2Fo8%2Fid & openid.ns = http:%2F%2Fspecs.openid.net%2Fauth ........

我在这里做错了什么?

注: 我之所以尝试指定RETURNURL是我的web位于〜\ WebApi.aspx这不是我想要当我的要求去做的土地.. 我想看看组件, ILSpy但“CreateRequest”方法或多或少为空。

回答

1

如果仔细查看错误消息中的两个URL,您会看到明确提到default.aspx,而另一个则没有。这就是打破它。尝试调整自己的显式return_to以包含页面名称,它可能会开始为您工作。

另一方面,从响应中获取Location HTTP头并将其发送给Javascript是不可靠的。某些OpenID请求太大,不适合单个URL,Location标头将为空。相反,响应对象具有自我提交HTML表单的有效内容。如果它发生跨越最大大小阈值,您的代码将失败。但在这里探索你的选择值得一个专门的Stackoverflow问题。 :)

+0

感谢您的洞察力,它解决了我的问题! – Peter 2012-01-20 23:56:21