【问题标题】:How to integrate OpenId with ASP.Net Membership in MVC如何在 MVC 中将 OpenId 与 ASP.Net Membership 集成
【发布时间】:2009-07-27 05:42:39
【问题描述】:

我正在使用 MVC Storefront 中的以下代码在 MVC 中测试 OpenId。 如何将其与我的 ASP.Net Membership 集成,以便我可以使用角色并将用户名保存在我的表中?我相信 SO 也在使用类似的东西。

    public ActionResult OpenIdLogin()
    {
        string returnUrl = VirtualPathUtility.ToAbsolute("~/");
        var openid = new OpenIdRelyingParty();
        var response = openid.GetResponse();
        if (response == null)
        {
            // Stage 2: user submitting Identifier
            Identifier id;
            if (Identifier.TryParse(Request["openid_identifier"], out id))
            {
                try
                {
                    IAuthenticationRequest req = openid.CreateRequest(Request["openid_identifier"]);

                    var fetch = new FetchRequest();
                    //ask for more info - the email address
                    var item = new AttributeRequest(WellKnownAttributes.Contact.Email);
                    item.IsRequired = true;
                    fetch.Attributes.Add(item);
                    req.AddExtension(fetch);

                    return req.RedirectingResponse.AsActionResult();
                }
                catch (ProtocolException ex)
                {
                    ViewData["Message"] = ex.Message;
                    return View("Logon");
                }
            }
            else
            {
                ViewData["Message"] = "Invalid identifier";
                return View("Logon");
            }
        }
        else
        {
            // Stage 3: OpenID Provider sending assertion response
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:

                    var fetch = response.GetExtension<FetchResponse>();
                    string name = response.FriendlyIdentifierForDisplay;
                    if (fetch != null)
                    {
                        IList<string> emailAddresses = fetch.Attributes[WellKnownAttributes.Contact.Email].Values;
                        string email = emailAddresses.Count > 0 ? emailAddresses[0] : null;
                        //don't show the email - it's creepy. Just use the name of the email
                        name = email.Substring(0, email.IndexOf('@'));
                    }
                    else
                    {

                        name = name.Substring(0, name.IndexOf('.'));
                    }

                    //FormsAuthentication.SetAuthCookie(name, false);
                    SetCookies(name, name);
                    AuthAndRedirect(name, name);

                    if (!string.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                case AuthenticationStatus.Canceled:
                    ViewData["Message"] = "Canceled at provider";
                    return View("Logon");
                case AuthenticationStatus.Failed:
                    ViewData["Message"] = response.Exception.Message;
                    return View("Logon");
            }
        }
        return new EmptyResult();

    }

    ActionResult AuthAndRedirect(string userName, string friendlyName)
    {
        string returnUrl = Request["ReturnUrl"];
        SetCookies(userName, friendlyName);

        if (!String.IsNullOrEmpty(returnUrl))
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }

【问题讨论】:

    标签: asp.net-mvc asp.net-membership openid


    【解决方案1】:

    在 StackOverflow 上已经有几个类似您的问题了。 This one 似乎特别相似。

    如果您已经在为您的网站使用 Membership 提供程序并且只是向其中添加 OpenID,那么我想您现在被 Membership 卡住了,可以使用我链接到的问题的答案之一来获得可能为您工作的半体面的会员提供商。

    但是,如果您正在编写一个新站点,并且只想“使用角色并在我的表中为用户保存用户名”,那么就不要使用 ASP.NET 成员资格。这太不值得了!它不符合 OpenID 的无密码范式,只会引起比其他任何事情更多的悲痛。如果您自己不害怕一点点数据库访问,那就这样做吧。您只需发出自己的FormsAuthentication.RedirectFromLoginPageFormsAuthentication.SetAuthCookie 调用并传入用户填写的角色,即可非常轻松地获得角色行为。

    【讨论】:

    • 我正在玩一个新的实现,所以我并没有特别嫁给 ASP.NET Membership。您能否提供示例代码来说明如何按照您提到的方式进行操作?
    • 我将在此处留下“我也是”的示例代码。你知道吗?
    • DotNetOpenAuth 附带的所有示例和项目模板都按照我提到的方式进行。
    【解决方案2】:

    开放 ID 提供者将返回有关用户的数据。如果您不请求/要求特定的信息令牌,那么您将获得的只是用户的显示名称和身份 URL。

    根据您使用的开放 id 库,您可以请求诸如 FirstName LastName、DOB 之类的令牌(如果您真的关心的话),如果用户提供了有关他们选择的身份的信息,那么您会得到它返回给您.

    然后您可以使用它在会员系统中创建一个新用户。您可能必须给他们一个虚拟密码才能绕过 Membership API 的要求。

    要验证登录,请提供一个包含用户名和密码的表单,另一个包含身份 URL 的表单。通过 open id 验证用户后,尝试在 Membership API 中通过用户名(身份 url)查找用户。如果不存在,则创建它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 2011-04-11
      相关资源
      最近更新 更多