gpt4 book ai didi

asp.net-mvc - DotNetOpenAuth 如何从 OpenId 检索电子邮件和名称

转载 作者:行者123 更新时间:2023-12-04 05:39:20 25 4
gpt4 key购买 nike

我在 MVC 3 中使用 DotNetOpenAuth。我能够使用 OpenId 进行身份验证。

所以我指向 Controller 的 URL 附加了一个变量 OpenId=

下面是一个例子:

https://www.google.com/accounts/o8/id?id=DFtOawDkCUoLb3YxPzmrEI59-JiSZiAeR-NWw-0

http://mysite.com/Account/Register?OpenID= ......

如何获取 OpenId 帐户的电子邮件和名称并显示在 View 中?

switch (response.Status)
{
case AuthenticationStatus.Authenticated:
LogOnModel lm = new LogOnModel();
lm.OpenID = response.ClaimedIdentifier;
// Check if user exist
MembershipUser user = MembershipService.GetUser(lm.OpenID);
if (user != null)
{
lm.UserName = user.UserName;
FormsService.SignIn(user.UserName, false);
}

return View("LogOn", lm);

case AuthenticationStatus.Canceled:
ViewBag.Message = "Canceled at provider";
return View("LogOn");
case AuthenticationStatus.Failed:
ViewBag.Message = response.Exception.Message;
return View("LogOn");
}

最佳答案

var fetchResponse = response.GetExtension<FetchResponse>();
string email = String.Empty;
if (fetchResponse != null)
{
email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email);
}

但是不要忘记在发送请求时询问它,以便用户可以在身份验证阶段授权您使用他的 OpenId 提供程序获取此信息:
IAuthenticationRequest request = openid.CreateRequest(openidurl);
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
request.AddExtension(fetch);
request.RedirectToProvider();

关于asp.net-mvc - DotNetOpenAuth 如何从 OpenId 检索电子邮件和名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11467147/

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