你可以做到這一點 -
FormsAuthenticationTicket _ticket = new FormsAuthenticationTicket(_version, _name, _issueDate, _expirationDate, _isPersistent, _userData, _cookiePath);
string _encryptedTicket = FormsAuthentication.Encrypt(_ticket);
HttpCookie _cookie = new HttpCookie("customticket", _encryptedTicket);
HttpContext.Current.Response.Cookies.Add(_cookie);
然後你就可以編寫代碼來檢查傳入的請求,看看他們是否有這個Cookie -
HttpCookie _cookie = HttpContext.Current.Request.Cookies["customticket"];
if(_cookie){
_encryptedTicket = _cookie.Value;
FormsAuthenticationTicket _ticket = FormsAuthentication.Decrypt(_encryptedTicket);
if(!_ticket.Expired) {
IIdentity _identity = new FormsIdentity(_ticket);
IPrincipal _principal = new GenericPrincipal(_identity, new string[0]); //Identity plus string of roles.
}
}
else{
//dostuff
}
寫的問題後,這是我最後的辦法一起去。謝謝! – 2012-03-22 18:53:59
極好的解決方案。我已經在我們的MVC 5應用程序中採用了這一點。 – Tommassiov 2015-02-25 11:27:26