gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:50:50 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com