FormsAuthentication - handling a change of username

陌路散爱 提交于 2019-12-04 18:33:28

问题


My ASP.NET MVC web application allows administrators to change their own, or other users' usernames.

Users are logged in by calling FormsAuthentication.SetAuthCookie(userName [string], createPersistentCookie [bool]). They are logged out by calling FormsAuthentication.SignOut(). I understand that after updating the username I'd need to sign them out and back in again. But how do I retrieve the existing value of createPersistentCookie? e.g. how do I retain their original 'remember me' setting when signing them back in?


回答1:


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
}


来源:https://stackoverflow.com/questions/6301342/formsauthentication-handling-a-change-of-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!