18

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).

Trying to give users an opportunity to change their UserName. There seems to be no function for that under AuthenticationIdentityManager, so I change the data using EF (get User object for current user, change UserName and save changes).

The problem is that authentication cookies remain unchanged, and the next request fails as there is no such user.

With forms authentication in the past I used the following code to solve this.

var formsAuthCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
var isPersistent = FormsAuthentication.Decrypt(formsAuthCookie.Value).IsPersistent;
FormsAuthentication.SetAuthCookie(newUserName, isPersistent);

What should I do with asp.net identity to update the cookies?

UPDATE

The following code seems to update the authentication cookie.

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});

The remaining problem is: how to extract IsPersistent value from current authentication cookie?

Mrchief
  • 75,126
  • 20
  • 142
  • 189
aleyush
  • 644
  • 1
  • 6
  • 19

1 Answers1

16

How do you login/authenticate a user with Asp.Net MVC5 RTM bits using AspNet.Identity?

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

For the RC1, You can use the similar code.

AuthenticationManager.SignOut();
IdentityManager.Authentication.SignIn(AuthenticationManager, user.UserId, isPersistent:false);

For persistent value, you need to access the authentication cookie and retrieve the status.

Updated:

Use appropriate AuthenticationType used in place of "Bearer". Also make sure while issuing the signin ticket, you are setting the AuthenticationProperties.IsPersistent.

bool isPersistent=false;
var authContext = await Authentication.AuthenticateAsync("Bearer");
if (authContext != null)
{
   var aProperties = authContext.Properties;
   isPersistent = aProperties.IsPersistent;
}
Community
  • 1
  • 1
jd4u
  • 5,789
  • 2
  • 28
  • 28
  • This code may work for RTM (not widely available yet). In RC1 there are no DefaultAuthenticationTypes and UserManager.CreateIdentityAsync(). – aleyush Oct 14 '13 at 09:28
  • The second question: how can I get current IsPersistent value (my goal is just to change UserName, not anything else)? – aleyush Oct 14 '13 at 09:44
  • Before release of VS2013, it is good to keep trials with the nightly build. Few of the comments here from developers states the many RC1 classes are not available in RTM that is going to be released with VS2013 in Nov. – jd4u Oct 14 '13 at 13:52
  • 1
    I have upgraded to nightly builds, but the problem is still here: how can I get current IsPersistent value? – aleyush Oct 15 '13 at 14:00
  • Your last update helped (changed "Bearer" to DefaultAuthenticationTypes.ExternalCookie). Marked as answer. – aleyush Oct 17 '13 at 10:30
  • Important: In more recent OWIN Projects, the default template code uses DefaultAuthenticationTypes.ApplicationCookie, not DefaultAuthenticationTypes.ExternalCookie - that will make everything above effectively do nothing if you fail to swap this little detail in your code. – Chris Moschini Feb 07 '18 at 18:25