c# - 通过 c# .net 使用 gmail 帐户登录

标签 c# .net google-api openid dotnetopenauth

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

我使用的代码是

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 status was failed即使我提供了正确的gmail帐户用户名和密码,身份验证后也是如此

等待有值(value)的回复和评论

最佳答案

按照您的要求。您应该尝试此代码或查看此链接: Gmail credentials for Authentication of ASP.net Website

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();

}

关于c# - 通过 c# .net 使用 gmail 帐户登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15354278/

相关文章:

c# - 使用 LINQ to SQL;如何在不设置所有列值的情况下在表中插入行?

c# - Web 应用程序 Active Directory 用户角色与 User.IsInRole

c# - 使用 NserviceBus.Testing 如何在代码未通过消息处理程序启动时测试已发布的事件

google-app-engine - 如何获取 Google Group Settings API 的 groupUniqueId?

html - 谷歌字体不适用于任何浏览器或平板电脑

c# - 可访问性不一致 : base class is less accessible than class

c# - 验证有效的 SQL 字符串

c# - 将我的属性值与模型类中另一个属性的值进行比较的自定义验证属性

c# - 编写文件 C# (.NET) 或 PERL 哪个更快?

android - 如何在限制特定位置的android中使用google places api?