微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – 如何将OpenId与ASP.Net成员集成在MVC中

我使用MVC Storefront中的以下代码在MVC中测试OpenId.如何将其与我的ASP.Net成员集成,以便我可以使用角色并在我的表中为用户保存用户名?我相信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");
        }
    }

解决方法

在StackOverflow上有几个像你这样的问题. This one似乎特别相似.

如果你已经在为你的网站使用会员资格提供商并且只是添加了OpenID,那么我猜你现在已经被困在会员资格中并且可以使用我链接到的问题的答案之一来获得半正式可能适合您的会员提供商.

但是,如果您正在编写一个站点并且只是想要“使用角色并在我的表中为用户保存用户名”,那么请不要使用ASP.NET成员资格.这不值得!它不适合OpenID的无密码范例,只会导致比其他任何事情更悲伤.如果你不害怕自己的一点点数据库访问,那就这样做吧.只需发出自己的FormsAuthentication.RedirectFromLoginPage或FormsAuthentication.SetAuthCookie调用并传入用户填充的角色,即可轻松获得角色行为.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐