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调用并传入用户填充的角色,即可轻松获得角色行为. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 错误消息401.2:未授权:由于服务器配置,登录失
- 为什么我的客户端去服务器来检查在使用ASP.NET MVC包时是否
- asp.net – 为什么我需要PUT或DELETE Http Verbs?
- asp.net-mvc – 如何使用Visual Studio 2013和Entity Frame
- asp.net-mvc-3 – 从自定义授权属性访问角色
- asp.net-mvc – 如何将我的对象保存回LINQ to SQL中的数据库
- MVC学习一:EF
- asp.net – 自动插入HTML属性的双引号
- asp.net-mvc – 如何创建从httpget获取相同参数的httppost?
- 在ASP,C#和VB.Net中如何检索当前行号
推荐文章
站长推荐
- 在asp.net mvc 2.0中使用Html.DropDownListFor帮
- asp.net – 如何在Entity框架中添加表?
- asp.net-mvc – System.Web.HttpRequestBase不包
- asp.net-mvc – 在不更新的其他.config文件上的A
- 有没有办法远程调用ASP.NET开发Web服务器?
- asp.net – 如何阻止MonoDevelop在调试时打开Saf
- asp.net-mvc-3 – RavenDb,无法访问文件,文件被锁
- asp.net – Url重写与路由
- asp.net-mvc – 实体框架 – 如何防止创建列的模
- 在ASP.NET中,获取基本UR1请求的最快方法是什么?
热点阅读