gpt4 book ai didi

c# - 在 ASP.Net MVC3 中使用 OpenID,我从哪里获取用户数据?

转载 作者:可可西里 更新时间:2023-11-01 09:15:49 25 4
gpt4 key购买 nike

我意识到 OpenID 有点庞然大物,或者比典型的注册表更复杂,但我觉得我在这里遗漏了一些东西。

根据这个question ,我应该保存提供商给我的唯一标识符 key 。

The provider will give you a unique ID for each user - this you need to save. It's how you will match up the user that just logged in with a record in your database.

my code (taken from the MVC portion) ,此唯一 ID 在 LogOn() 操作方法中的开关内给出:

public ActionResult LogOn()
{
var openid = new OpenIdRelyingParty();
IAuthenticationResponse response = openid.GetResponse();

if (response != null)
{
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
FormsAuthentication.RedirectFromLoginPage(
response.ClaimedIdentifier, false); // <-------- ID HERE! "response.ClaimedIdentifier"
break;
case AuthenticationStatus.Canceled:
ModelState.AddModelError("loginIdentifier",
"Login was cancelled at the provider");
break;
case AuthenticationStatus.Failed:
ModelState.AddModelError("loginIdentifier",
"Login failed using the provided OpenID identifier");
break;
}
}

return View();
}

[HttpPost]
public ActionResult LogOn(string loginIdentifier)
{
if (!Identifier.IsValid(loginIdentifier))
{
ModelState.AddModelError("loginIdentifier",
"The specified login identifier is invalid");
return View();
}
else
{
var openid = new OpenIdRelyingParty();
IAuthenticationRequest request = openid.CreateRequest(Identifier.Parse(loginIdentifier));

// Require some additional data
request.AddExtension(new ClaimsRequest
{
BirthDate = DemandLevel.NoRequest,
Email = DemandLevel.Require,
FullName = DemandLevel.Require
});

return request.RedirectingResponse.AsActionResult();
}
}

我是否将此标识符用于 FormsAuthentication.SetAuthCookie(IDHERE, true);

如果我还想保存用户信息,例如电子邮件、姓名、昵称或其他信息,该怎么办?我如何从依赖方获取这些数据集合?如果此过程取决于我使用的提供商,我使用的是 Steam OpenID 提供商:

http://steamcommunity.com/openid http://steamcommunity.com/dev

最佳答案

当您成功登录后,您可以对接收到的数据做任何您喜欢的事情:唯一标识符以及您请求的声明。

  1. 将收集到的数据存储在数据库记录中。
  2. 将其保存在 cookie 中(将其作为 token 发送给您的服务(如果有)或在您的 RP(依赖方)中使用)。
  3. 将它与通用成员(member)提供程序或简单的 Sql 提供程序一起使用。

这是在 Controller 中执行第二个操作的方式:

    [AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
public ActionResult LogOnPostAssertion(string openid_openidAuthData)
{
IAuthenticationResponse response;
if (!string.IsNullOrEmpty(openid_openidAuthData))
{
var auth = new Uri(openid_openidAuthData);
var headers = new WebHeaderCollection();
foreach (string header in Request.Headers)
{
headers[header] = Request.Headers[header];
}

// Always say it's a GET since the payload is all in the URL, even the large ones.
HttpRequestInfo clientResponseInfo = new HttpRequestInfo("GET", auth, auth.PathAndQuery, headers, null);
response = this.RelyingParty.GetResponse(clientResponseInfo);
}
else
{
response = this.RelyingParty.GetResponse();
}
if (response != null)
{
switch (response.Status)
{
case AuthenticationStatus.Authenticated:
var token = RelyingPartyLogic.User.ProcessUserLogin(response);
this.FormsAuth.SignIn(token.ClaimedIdentifier, false);
string returnUrl = Request.Form["returnUrl"];
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
case AuthenticationStatus.Canceled:
ModelState.AddModelError("OpenID", "It looks like you canceled login at your OpenID Provider.");
break;
case AuthenticationStatus.Failed:
ModelState.AddModelError("OpenID", response.Exception.Message);
break;
}
}

关于您可以如何处理接收到的数据的其他提示:通过在您的依赖方数据库(您的应用程序)的 UserLogin 表中创建用户登录记录。您将能够在用户下次访问您的应用程序时验证身份验证和用户状态。您还可以在第一次将他重定向到特定页面以收集 OPenID 提供商未提供的更具体数据(例如:年龄或性别)。您可以跟踪所有用户登录(OpenID (steam)、google、liveID)并将它们链接到用户。这将允许您的唯一用户使用他也喜欢的任何身份验证提供程序登录。

有关使用 Open ID 身份验证器的完整示例,您可以查看 OpenId for MVC2 项目 1我从中提取了前面的示例。

关于c# - 在 ASP.Net MVC3 中使用 OpenID,我从哪里获取用户数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11531013/

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