Cài đặt Im FormsAuthenticationTicket trong phương thức Đăng nhập để tạo cookie xác thực theo cách thủ công. Làm cách nào để xác thực cookie xác thực đó và gán nó cho đối tượng Current.User. Nó được thực hiện trong trang Global.asax?Asp.Net MVC FormsAuthenticationTicket
đang Logon:
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");
Làm thế nào để tôi đọc cookie này và xác nhận người sử dụng? mã của tôi cho đến nay trong tệp 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;
}
cảm ơn rất nhiều đã giải quyết được vấn đề. – ace