簡體   English   中英

DotNetOpenAuth和Ajax

[英]DotNetOpenAuth and ajax

嗨,我已經創建了一個網站,我在其中登錄了OpenId。 當您按下登錄按鈕時,我會調用ajax方法,該方法基本上是通過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:// localhost:33386 /?action = verify&dnoa.userSuppliedIdentifier = https:%2F%2Fwww.google.com%2Faccounts%2Fo8%2Fid)與實際網址(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”方法或多或少是空的。

如果仔細查看錯誤消息中的兩個URL,您會發現其中一個明確提到default.aspx ,而另一個則沒有。 那就是打破它的原因。 嘗試調整您自己的顯式return_to以包括頁面名稱,它可能會開始為您服務。

另一方面,從響應中獲取Location HTTP標頭並將其發送到Javascript是不可靠的。 某些OpenID請求太大,以致無法放入單個URL,並且Location標頭為空。 而是,響應對象具有自提交HTML表單的有效負載。 如果您的代碼碰巧超過了最大大小閾值,它將失敗。 但是,這里探討您的選擇值得一個專門的Stackoverflow問題。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM