通过C#.NET使用Gmail帐户登录 [英] Login with gmail account through c# .net

查看:261
本文介绍了通过C#.NET使用Gmail帐户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DotNetOpenID DLL通过C#.NET登录通过Gmail验证我的示例应用程序

i am using DotNetOpenID dll for logging my sample application through gmail authentication through c# .net

这是我用code是

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页面,并验证为我所需要的。

it works well when i click the gmail login button it goes to the gmail page and authenticate as i need.

但我的问题是 AuthenticationStatus.Authenticated地位失败。身份验证始终,即使我给Gmail帐户的正确的用户名和密码后,

but my problem is AuthenticationStatus.Authenticated status was failed after authentication always even though i am giving correct username and password of gmail account

等待宝贵的反应和意见

推荐答案

与参数的requirement.You应该试试这个code或看到该链接: <一href="http://stackoverflow.com/questions/9199958/gmail-credentials-for-authentication-of-asp-net-website">Gmail凭据ASP.net网站的身份验证

As par your requirement.You should try this code or see this link : 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#.NET使用Gmail帐户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆