Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to change authentication cookies after changing UserName of current user with asp.net identity
    primarykey
    data
    text
    <p>Using asp.net identity version 1.0.0-rc1 with Entity Framework 6.0.0-rc1 (the ones that come with Visual Studio 2013 RC).</p> <p>Trying to give users an opportunity to change their <code>UserName</code>. There seems to be no function for that under <code>AuthenticationIdentityManager</code>, so I change the data using EF (get User object for current user, change UserName and save changes).</p> <p>The problem is that authentication cookies remain unchanged, and the next request fails as there is no such user.</p> <p>With forms authentication in the past I used the following code to solve this.</p> <pre><code>var formsAuthCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; var isPersistent = FormsAuthentication.Decrypt(formsAuthCookie.Value).IsPersistent; FormsAuthentication.SetAuthCookie(newUserName, isPersistent); </code></pre> <p>What should I do with asp.net identity to update the cookies?</p> <p><strong>UPDATE</strong></p> <p>The following code seems to update the authentication cookie.</p> <pre><code>var identity = new ClaimsIdentity(User.Identity); identity.RemoveClaim(identity.FindFirst(identity.NameClaimType)); identity.AddClaim(new Claim(identity.NameClaimType, newUserName)); AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant (new ClaimsPrincipal(identity), new AuthenticationProperties {IsPersistent = false}); </code></pre> <p>The remaining problem is: how to extract <code>IsPersistent</code> value from current authentication cookie?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload