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

Onl*_*ere 10 c# openid dotnetopenauth asp.net-mvc-3 steam

我发现OpenID有点像一个庞大的,或者比典型的注册形式更复杂,但我觉得我在这里缺少一些东西.

根据这个问题,我应该保存我的提供商给出的唯一标识符密钥.

提供商将为每个用户提供一个唯一的ID - 您需要保存.这是您将刚刚登录的用户与数据库中的记录进行匹配的方式.

我的代码中(取自MVC部分),这个唯一的ID在LogOn()action方法的开关内给出:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

如果我还想保存用户信息,例如电子邮件,姓名,昵称等等,该怎么办?如何从依赖方获取此数据集?如果此过程依赖于我正在使用的提供程序,我使用的是Steam OpenID提供程序:

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

Oua*_*ATA 1

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

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

以下是您应该如何在控制器中执行第二个操作:

    [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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

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