2011-06-10 58 views
10

我的ASP.NET MVC Web应用程序允许管理员更改他们自己的或其他用户的用户名。FormsAuthentication - 处理用户名更改

用户通过致电FormsAuthentication.SetAuthCookie(userName [string], createPersistentCookie [bool])登录。他们通过致电FormsAuthentication.SignOut()注销。我知道在更新用户名后,我需要将它们签出并重新登录。但是,如何检索createPersistentCookie现有的值?例如在签名时如何保留原始的“记住我”设置?

回答

8
var cookieName = FormsAuthentication.FormsCookieName; 
var request = HttpContext.Current.Request; 
var cookie = request.Cookies.Get(cookieName); 
if (cookie == null) 
    return; 

try 
{ 
    var ticket = FormsAuthentication.Decrypt(cookie.Value); 

    //This should give you what you want... 
    bool isPersistent = ticket.IsPersistent; 
} 
catch (Exception ex) 
{ 
    //Logging 
}