Hi,
I'm using ASP.NET Identity 2.0 Beta 1. I have some problems with using my own custom claims (I don't think it is Beta specific).
I added this code to SignInAsyn in AccountController:
userIdentity.AddClaim(new Claim("FullName", "My full name"));
Here the complete method:
private async Task SignInAsync(ApplicationUser user, bool isPersistent, bool rememberBrowser) { // Clear any partial cookies from external or two factor partial sign ins AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie); var userIdentity = await user.GenerateUserIdentityAsync(UserManager); // Here my custom claims userIdentity.AddClaim(new Claim("FullName", "My full name")); userIdentity.AddClaim(new Claim("EmployeeId", "My employee id")); if (rememberBrowser) { var rememberBrowserIdentity = AuthenticationManager.CreateTwoFactorRememberBrowserIdentity(user.Id.ToString()); AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity, rememberBrowserIdentity); } else { AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, userIdentity); } }
My problem is, that whenever my authentication cookie is expired, I'm loosing my custom claims (the user is still authenticated, only my custom claims are not available anymore).
This is my cookie configuration:
// Enable the application to use a cookie to store information for the signed in user // and to use a cookie to temporarily store information about a user logging in with a third party login provider // Configure the sign in cookie app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), SlidingExpiration = true, Provider = new CookieAuthenticationProvider { // Enables the application to validate the security stamp when the user logs in. // This is a security feature which is used when you change a password or add an external login to your account. OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>( validateInterval: TimeSpan.FromSeconds(15), regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager), getUserIdCallback: (user) => Guid.Parse(user.GetUserId()) ) } });
For better testing I set the validateInterval to 15 seconds. After this time all my custom claims are gone :(
I found on google other people with similar/same problem, but I cannot solve this problem. See here: http://stackoverflow.com/questions/18883038/asp-net-added-claims-are-missing
How can I use/configure in the link mentioned ClaimsAuthenticationManager in my project. I'm using ASP.NET MVC 5 with ASP.NET Identity 2.0 Beta 1.
Regards, Daniel