繁体   English   中英

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

[英]Using OpenID in ASP.Net MVC3, where do I get the user data?

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

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

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

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

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中(或者将其作为令牌发送给您的服务,如果有的话或在您的RP中使用它(Reliant party))。
  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;
            }
        }

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM