2014-12-04 64 views
1

我在asp.net内存中存在“缓存”问题,当我更改密码,名称,任何声明时,我必须重新启动应用程序以验证更改。asp net identity EF

我有这个在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"); 
    } 
} 

登录:

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 
} 

避免其他方法

例如,对于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 

ŧ这里是我遵循的步骤:

  • 登录应用
  • 更改密码
  • 注销申请
  • 登录使用新的密码(如表发生变化,是正确的变化)
  • 误差密码
  • 用较旧的密码登录(表中的旧密码不存在)
  • 登录成功
  • 重新启动应用
  • 新密码现在是有效的

,当我在BBDD改变ASP.NET的身份的任何值,也存在同样的问题

任何想法吗?

谢谢!

+0

我不确定我是否按照你的问题。你能重新解释一下这个问题吗? – trailmax 2014-12-04 10:04:49

+0

有我遵循的步骤: 登录应用 更改密码 退出应用 登录使用新的密码(如表发生变化,是正确的变化) 错误与旧的密码,密码 登录(旧密码表不存在) 登录成功 重新启动应用 新密码现在是有效的 有我跟着 – David91 2014-12-04 10:32:10

+0

所以你说的密码更改后,您可以使用旧密码和新密码后,才适用登录的步骤应用重启? – trailmax 2014-12-04 10:59:28

回答

1

如果我没有记错,我添加相同的问题,因为其中一个上下文被保持,另一个上下文在每次调用时重新创建。

如果您检查一个将不会从数据库中获得正确的值,可能是ApplicationOAuthProvider

尝试重新创建ApplicationOAuthProvider上的每个呼叫的上下文。

+0

这听起来很合理! – trailmax 2014-12-04 14:14:37

+0

我正在尝试您的回复,很可能您有权利,+1 – David91 2014-12-04 17:07:58