我正在用Identity 2.0在MVC 5中重写我的MVC 4表单身份验证应用程序。
当前,该站点使用专有的单点登录方法,该方法对POST请求中的传入参数进行解密,并根据内容的有效性使用FormsAuthentication.SetAuthCookie设置身份验证Cookie并将用户重定向到安全的内容。
现在,我正在使用ASP.Net Identity,那么FormsAuthentication.SetAuthCookie功能的合适替代品是什么?请记住,我不想将这些用户中的任何一个持久化到数据库中。它们应该只存在于令牌/ cookie中。
得到它了!我在这篇有用的文章中找到了解决方案。
基本上,您创建一个ClaimsIdentity,然后使用OWINContext中的AuthenticationManager来“登录”身份并创建身份验证cookie。
像这样:
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "Brock"));
claims.Add(new Claim(ClaimTypes.Email, "[email protected]"));
var id = new ClaimsIdentity(claims,DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(id);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句