1

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!!

4
  • I'm not sure I follow your problem. Can you please rephrase the question? Commented Dec 4, 2014 at 10:04
  • 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 There are the steps I followed Commented Dec 4, 2014 at 10:32
  • so you are saying that after password change, you can login with the old password and new password only works after the application restart? Commented Dec 4, 2014 at 10:59
  • Yes, just right, trailmax Commented Dec 4, 2014 at 12:14

1 Answer 1

1

If I recall correctly I add the same issue because one of the contexts was being persisted and the other recreated on every call.

If you check one will not have the correct value from the DB, probably ApplicationOAuthProvider.

Try recreating the context for every call on the ApplicationOAuthProvider.

Sign up to request clarification or add additional context in comments.

2 Comments

That sounds very plausible!
I am trying your response, is probable you have right, +1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.