2014-12-04 129 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