0

我陷入了類似這樣的情況。Asp.Net身份 - SetPasswordHashAsync不能正常工作

我正在實施標識2.0與我的新MVC應用程序。我想先用數據庫先用方法用用戶ID作爲int

要做到這一點,我創建

  • 自定義用戶存儲 - MyUserStore

    public class MyUserStore : IUserLoginStore<AspNetUser>, IUserClaimStore<AspNetUser>, IUserRoleStore<AspNetUser>, IUserPasswordStore<AspNetUser>, IUserSecurityStampStore<AspNetUser>, IUserStore<AspNetUser>, IDisposable //where AspNetUser : AspNetUser 
        { 
         private readonly DemoPermissionDBContext _context; 
         //private readonly Func<string, string> _partitionKeyFromId; 
    
         public MyUserStore(DemoPermissionDBContext dbContext) 
         { 
          _context = dbContext; 
         } 
    
         public Task SetPasswordHashAsync(AspNetUser user, string passwordHash) 
         { 
          user.PasswordHash = passwordHash; 
          return Task.FromResult(0); 
         } 
    
         //And other methods implementation 
        } 
    

這裏AspNetUser是由EF生成的用戶類和我做了它繼承來自Identity.IUser

  • 定製用戶管理器 - AppUserManager

     public class AppUserManager : Microsoft.AspNet.Identity.UserManager<AspNetUser> 
        { 
          MyUserStore _store = null; 
    
          public AppUserManager(MyUserStore store) : base(store) 
          { 
           _store = store; 
          } 
    
        } 
    
  • 定製密碼散列器 - MyPasswordHasher

    public class MyPasswordHasher : IPasswordHasher 
    { 
        public string HashPassword(string password) 
        { 
         return password; 
        } 
    
        public PasswordVerificationResult VerifyHashedPassword(
         string hashedPassword, string providedPassword) 
        { 
         if (hashedPassword == HashPassword(providedPassword)) 
          return PasswordVerificationResult.Success; 
         else 
          return PasswordVerificationResult.Failed; 
        } 
    } 
    
  • 自定義聲明PRINICIPAL - AppClaimsPrincipal

    public class AppClaimsPrincipal : ClaimsPrincipal 
    { 
         public AppClaimsPrincipal(ClaimsPrincipal principal):base(principal) 
         { 
    
         } 
    
         public int UserId 
         { 
          get { return int.Parse(this.FindFirst(ClaimTypes.Sid).Value); 
         } 
    } 
    

    現在,當我註冊用戶,的UserManager .CreateAsync shoul d實際將用戶保存到數據庫之前,會自動調用SetPasswordHashAsync。與此同時,SetSecurityStampAsync被正確調用。

我在我的AccountController做的就是這樣的事情,

public AppUserManager UserManager { get; private set; } 

public AccountController(AppUserManager userManager) 
{ 
    this.UserManager = userManager; 
    this.UserManager.PasswordHasher = new MyPasswordHasher(); 

} 

這是怎麼了我已經實現了身份的用戶int主鍵,爲用戶表和數據庫第一種方法。

問:

當我的用戶插入數據庫,它包含了沒有密碼哈希值。該記錄具有空密碼散列。當我調試代碼時,我發現SetPasswordHashAsync不是隻調用。它沒有執行這個小節。什麼應該導致這個問題?我已經逐行檢查了整個代碼行,但沒有成功。有什麼我需要添加,在web.config使用自定義密碼哈希?

我不知道我是否錯過了一些東西。任何幫助是極大的讚賞。

+0

我有一個類似的問題,並發現這個問題..不知道它是否與你的問題相同,但最終(顯然在最後..)我發現我調用UserManager.CreateAsync(用戶)時我應該一直在調用'UserManager.CreateAsync(user,password)',否則密碼不會被使用,哈希函數也不會被調用。 – steve16351 2015-10-07 21:14:26

回答

1

我在userManager中看不到您設置MyPasswordHasher。Add

this.PasswordHasher = new MyPasswordHasher(); 

構造函數AppUserManager

此外,您無需重寫ClaimsPrincipal即可獲得UserId。這可通過User.Identity.GetUserId()獲得,位於Microsoft.AspNet.Identity命名空間中。

此外,我發現在IPrincipal上使用擴展方法可以更容易地獲得聲明的值 - 比自定義主體類型更容易。

+0

恐怕不是那麼容易。我已經查過了。 :( – 2015-02-25 01:21:22

相關問題