I am having a problem with "cache" in asp .net identity, when I change password, name, any claim, I must restart the application for validate the changes.
I have this in SecurityContext
public class SecurityContext : IdentityDbContext<IdentityUser> { public SecurityContext() : base("Db") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("security"); base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUser>() .ToTable("_Users"); modelBuilder.Entity<IdentityRole>() .ToTable("_Roles"); modelBuilder.Entity<IdentityUserRole>() .ToTable("_UsersRoles"); modelBuilder.Entity<IdentityUserClaim>() .ToTable("_UsersClaims"); modelBuilder.Entity<IdentityUserLogin>() .ToTable("_UsersLogins"); } } Login:
public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider { private readonly string _PublicClientId; private readonly Func<UserManager<IdentityUser>> _UserManagerFactory; private readonly Func<RoleManager<IdentityRole>> _RoleManagerFactory; #region Constructors public ApplicationOAuthProvider(string publicClientId, Func<UserManager<IdentityUser>> userManagerFactory, Func<RoleManager<IdentityRole>> roleManagerFactory ) { if (publicClientId == null) throw new ArgumentNullException("publicClientId"); _PublicClientId = publicClientId; if (userManagerFactory == null) throw new ArgumentNullException("userManagerFactory"); _UserManagerFactory = userManagerFactory; if (roleManagerFactory == null) throw new ArgumentNullException("roleManagerFactory"); _RoleManagerFactory = roleManagerFactory; } #endregion Constructors #region GrantResourceOwnerCredentials public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { using (var userManager = _UserManagerFactory()) { using (var roleManager = _RoleManagerFactory()) { var user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } // Start Login success var oAuthIdentity = await userManager.CreateIdentityAsync(user, context.Options.AuthenticationType); var cookiesIdentity = await userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); // Claims cookiesIdentity.AddClaim(new Claim(XpClaimTypes.Application, _SessionData.ApplicationName)); // Properties var properties = CreateProperties(user, roleManager); var ticket = new AuthenticationTicket(oAuthIdentity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(cookiesIdentity); // End Login success } } } #endregion GrantResourceOwnerCredentials } obviating others methods
For example the method for changePassword:
#region Password [HttpPut] [Authorize(Roles = AccountRoles.Superadministrador + "," + AccountRoles.Administrador)] public async Task<IHttpActionResult> Password(SetPasswordBindingModel model) { if (!ModelState.IsValid) return BadRequest(ModelState); var identity = await UserManager.FindByNameAsync((Thread.CurrentPrincipal.Identity as ClaimsIdentity).Name); var user = await UserManager.FindByIdAsync(model.Id); if (!( (identity.Roles.Any(x => x.Role.Name == AccountRoles.Superadministrador) && user.Roles.Any(x => x.Role.Name == AccountRoles.Administrador)) || (identity.Roles.Any(x => x.Role.Name == AccountRoles.Administrador) && user.Roles.Any(x => x.Role.Name == AccountRoles.Usuario)) )) throw new AuthenticationException(); // Delete password { var result = await UserManager.RemovePasswordAsync(model.Id); var errorResult = GetErrorResult(result); if (errorResult != null) return errorResult; } // Add password { var result = await UserManager.AddPasswordAsync(model.Id, model.Password); var errorResult = GetErrorResult(result); if (errorResult != null) return errorResult; } return Ok(); } #endregion Password There are the steps I followed:
- Login application
- Change the password
- Logout application
- Login with the new password (in table is changed, is correctly the change)
- Error with password
- Login with older password (the old password in table is not exists)
- Login successful
- Restart application
- The new password now is valid
The same problem is occurred when I change any value in BBDD of asp .net identity
Any Idea please?
Thanks!!