我在Logon方法中设置FormsAuthenticationTicket以手动创建身份验证cookie。如何验证身份验证cookie并为其分配Current.User对象。是在Global.asax页面中完成的吗?
登录代码:
FormsAuthenticationTicket Authticket = new
FormsAuthenticationTicket(1,
model.UserName,
DateTime.Now,
DateTime.Now.AddYears(1),
true,
"",
FormsAuthentication.FormsCookiePath);
string hash = FormsAuthentication.Encrypt(Authticket);
HttpCookie Authcookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
if (Authticket.IsPersistent) Authcookie.Expires = Authticket.Expiration;
Response.Cookies.Add(Authcookie);
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");如何读取此cookie并验证用户?到目前为止,我在global.asax文件中的代码:
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
FormsIdentity id = new FormsIdentity(authTicket);
GenericPrincipal principal = new GenericPrincipal(id,null);
Context.User = principal;
}发布于 2010-07-16 07:29:21
我将这种类型的代码移到了一个基本控制器中。在控制器类中有一个名为"OnAuthorization“的方法,它可以被覆盖。
已经有一段时间了,但我相信所有的请求(图片,css...等),在Global.asax中使用OnAuthorization方法。通过将授权向下推送到控制器,您只能获得对控制器/操作的请求
https://stackoverflow.com/questions/3260766
复制相似问题