2015-04-02 36 views
0

我在寫自定義實現IUserPasswordStore<Client>,但在登錄過程中我得到了FormatException。 好像方法GetPasswordHashAsync或誰調用此方法調用Microsoft.AspNet.Identity.Crypto.VerifyHashedPassword(String hashedPassword, String password)造成出現FormatException與消息在自定義IUserPasswordStore中的FormatException <Client>實現

長度無效了BASE-64字符數組或字符串。

這是我實現IUserPasswordStore<Client>

public partial class ClientRepository : IUserStore<Client>, IUserPasswordStore<Client>, IUserLockoutStore<Client, string> 
{ 
    public Task CreateAsync(Client user) 
    { 
     return Task.Factory.StartNew(() => Create(user)); 
    } 

    public Task UpdateAsync(Client user) 
    { 
     return Task.Factory.StartNew(() => Update(user)); 
    } 

    public Task DeleteAsync(Client user) 
    { 
     return Task.Factory.StartNew(() => Delete(user)); 
    } 

    public Task<Client> FindByIdAsync(string userId) 
    { 
     return Task.FromResult(Find(userId)); 
    } 

    public Task<Client> FindByNameAsync(string userName) 
    { 
     return Task.FromResult(FetchOne(new ClientByUsername(userName))); 
    } 

    public Task SetPasswordHashAsync(Client user, string passwordHash) 
    { 
     return Task.Factory.StartNew(() => user.Password = passwordHash); 
    } 

    public Task<string> GetPasswordHashAsync(Client user) 
    { 
     return Task.FromResult(user.Password); 
    } 

    public Task<bool> HasPasswordAsync(Client user) 
    { 
     return Task.Factory.StartNew(() => !string.IsNullOrEmpty(user.Password)); 
    } 

    public Task<DateTimeOffset> GetLockoutEndDateAsync(Client user) 
    { 
     return Task.FromResult<DateTimeOffset>(user.LockoutTo ?? DateTime.Now); 
    } 

    public Task SetLockoutEndDateAsync(Client user, DateTimeOffset lockoutEnd) 
    { 
     return Task.Factory.StartNew(() => user.LockoutTo = lockoutEnd.DateTime); 
    } 

    public Task<int> IncrementAccessFailedCountAsync(Client user) 
    { 
     return Task.FromResult(++user.LoginAttempts); 
    } 

    public Task ResetAccessFailedCountAsync(Client user) 
    { 
     return Task.Factory.StartNew(() => user.LoginAttempts = 0); 
    } 

    public Task<int> GetAccessFailedCountAsync(Client user) 
    { 
     return Task.Factory.StartNew(() => user.LoginAttempts); 
    } 

    public Task<bool> GetLockoutEnabledAsync(Client user) 
    { 
     return Task.FromResult(false); 
    } 

    public Task SetLockoutEnabledAsync(Client user, bool enabled) 
    { 
     return Task.FromResult(0); 
    } 



    public void Dispose() 
    { 
     database = null; 
     Manager = null; 
     context = null; 
    } 
} 

我的問題:在哪裏被稱爲Microsoft.AspNet.Identity.Crypto.VerifyHashedPassword(字符串hashedPassword,字符串密碼),我應該在GetPasswordHashAsync返回? 對於我在明文密碼使用啓動...

編輯:我想,也許我應該提供某種服務的哈希的,所以我添加虛擬實現IPasswordHasher爲我的目的是使用純文本

public class CustomPasswordHasher : IPasswordHasher 
{ 
    public string HashPassword(string password) 
    { 
     return password; //return password as is 
    } 

    public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword) 
    { 
     if (hashedPassword.Equals(providedPassword)) 
     { 
      return PasswordVerificationResult.Success; 
     } 
     return PasswordVerificationResult.Failed; 
    } 
} 

// and use it 

public class ApplicationUserManager : UserManager<Client> 
{ 
    public ApplicationUserManager(IUserStore<Client> store, IPasswordHasher hasher) 
     : base(store) 
    { 
     // ... 
     PasswordHasher = hasher; 
    } 
} 

但裏面CustomPasswordHasher斷點均未命中,所以propably我失去了一些東西......

+0

你是否缺少以下方法'返回等待Task.FromResult(user.Password);' – MethodMan 2015-04-02 19:45:19

+0

謝謝你的回覆,但它沒有幫助;還是一樣的例外...:/ – 2015-04-02 19:48:11

+0

你有沒有使用調試程序遍歷代碼..? – MethodMan 2015-04-02 19:49:16

回答

0

固定。我是對的,使用CustomPasswordHasher,但我有在Unity組合根的錯誤。大聲笑