加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:07:48 所属栏目:asp.Net 来源:网络整理
导读:我使用MVC Storefront中的以下代码在MVC中测试OpenId.如何将其与我的ASP.Net成员集成,以便我可以使用角色并在我的表中为用户保存用户名?我相信SO也在使用类似的东西. public ActionResult OpenIdLogin() { string returnUrl = VirtualPathUtility.ToAbsolut
我使用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调用并传入用户填充的角色,即可轻松获得角色行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读