通过c# .net登录gmail帐户



我使用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。认证状态失败认证后总是即使我给正确的用户名和密码的gmail帐户

等待有价值的回应和评论

按您的要求。你应该试试这个代码或者看看这个链接:用于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();
}

我不熟悉DotNetOpenID dll。但是,我建议使用Fiddler捕获在登录期间发送的POST中的数据,并确保在您的POST中发送正确的内容。c#在System.Net中提供了HttpWebRequest类和HttpWebResponse类。有什么原因你不使用这些从System.dll代替?

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

有一个很好的示例类来处理这个帖子中由水泥回答的请求

相关内容

  • 没有找到相关文章

最新更新