2011-06-10 40 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 
}