6

我正在使用 DotNetOpenID dll 通过 gmail 身份验证通过 c# .net 记录我的示例应用程序

我使用的代码是

protected void Page_Load(object sender, EventArgs e)
    {
        OpenIdRelyingParty rp = new OpenIdRelyingParty();
        var r = rp.GetResponse();
        if (r != null)
        {            
            switch (r.Status)
            {
                case AuthenticationStatus.Authenticated:
                    NotLoggedIn.Visible = false;
                    Session["GoogleIdentifier"] = r.ClaimedIdentifier.ToString();
                    Response.Redirect("About.aspx"); //redirect to main page of your website  
                    break;
                case AuthenticationStatus.Canceled:
                    lblAlertMsg.Text = "Cancelled.";
                    break;
                case AuthenticationStatus.Failed:
                    lblAlertMsg.Text = "Login Failed.";
                    break;
            }
        }
    }

    protected void OpenLogin_Click(object src, CommandEventArgs e)
    {
        string discoveryUri = e.CommandArgument.ToString();
        OpenIdRelyingParty openid = new OpenIdRelyingParty();
        var b = new UriBuilder(Request.Url) { Query = "" };
        var req = openid.CreateRequest(discoveryUri, b.Uri, b.Uri);
        req.RedirectToProvider();
    }

当我单击 gmail 登录按钮时,它运行良好,它转到 gmail 页面并根据我的需要进行身份验证。

但我的问题是 AuthenticationStatus.Authenticated 状态在身份验证后始终失败,即使我提供了正确的 gmail 帐户用户名和密码

等待有价值的回复和评论

4

2 回答 2

3

根据您的要求。您应该尝试此代码或查看此链接: 用于 ASP.net 网站身份验证的 Gmail 凭据

protected void Page_Load(object sender, EventArgs e)
{
    OpenIdAjaxRelyingParty rp = new OpenIdAjaxRelyingParty();
    var response = rp.GetResponse();
    if (response != null)
    {
        switch (response.Status)
        {
            case AuthenticationStatus.Authenticated:
                NotLoggedIn.Visible = false;
                Session["GoogleIdentifier"] = response.ClaimedIdentifier.ToString();

                var fetchResponse = response.GetExtension<FetchResponse>();
                Session["FetchResponse"] = fetchResponse;
                var response2 = Session["FetchResponse"] as FetchResponse;

                string UserName = response2.GetAttributeValue(WellKnownAttributes.Name.First) ?? "Guest"; // with the OpenID Claimed Identifier as their username.
                string UserEmail = response2.GetAttributeValue(WellKnownAttributes.Contact.Email) ?? "Guest";   

                Response.Redirect("Default2.aspx");
                break;

            case AuthenticationStatus.Canceled:
                lblAlertMsg.Text = "Cancelled.";
                break;
        }

    }
}
protected void OpenLogin_Click(object sender, CommandEventArgs e)
{

    string discoveryUri = e.CommandArgument.ToString();
    OpenIdRelyingParty openid = new OpenIdRelyingParty();

    var url = new UriBuilder(Request.Url) { Query = "" };
    var request = openid.CreateRequest(discoveryUri); // This is where you would add any OpenID extensions you wanted
    var fetchRequest = new FetchRequest(); // to fetch additional data fields from the OpenID Provider

    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
    fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.HomeAddress.Country);
    request.AddExtension(fetchRequest);

    request.RedirectToProvider();

}
于 2013-04-05T09:15:35.570 回答
0

我不熟悉 DotNetOpenID dll。但是,我建议使用 Fiddler 捕获登录期间发送的 POST 中的数据,并确保您在帖子中发送正确的内容。C #在. HttpWebRequest _ _ 您是否有任何理由不使用 System.dll 中的这些? HttpWebResponse System.Net

确保当您从 POST 中取回 cookie 时,将它们放入 cookie 集合中以供任何后续请求使用。

水泥回答的这篇文章中有一个很好的示例类来处理请求

于 2013-03-19T20:18:21.237 回答