Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FormsAuthentication - handling a change of username

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?

like image 805
Jake Petroules Avatar asked Jun 10 '11 02:06

Jake Petroules


1 Answers

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
}
like image 88
Charlino Avatar answered Sep 30 '22 04:09

Charlino