2017-05-29 74 views
0

我無法找到爲身份.net核心禁用自動哈希密碼的方式。 因爲這段代碼自動散列密碼:如何禁用身份.net核心的自動哈希密碼

var result = await _userManager.CreateAsync(user, model.Password); 
+3

爲什麼要禁用它?密碼應該**總是**被散列。 – DavidG

+0

您是否真的想將所有密碼公開給黑客併成爲下一次公開數據泄露?你確定你的公司能活下來嗎? –

+0

你是否試圖執行密碼恢復?這是非常不安全的,這就是爲什麼它不再在.NET中可用,而不僅僅是.NET Core。現今使用的最低限度是密碼重置。 –

回答

1

你可以編寫覆蓋UserManager

public class ApplicationUserManager : UserManager<IdentityUser> 
{ 
    public ApplicationUserManager(IUserStore<IdentityUser> store) 
     : base(store) 
    { 
     this.PasswordHasher = new CustomPasswordHasher(); 
    } 

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     var manager = new ApplicationUserManager(new UserStore<IdentityUser>(context.Get<ApplicationDbContext>()));   

     manager.PasswordHasher = new CustomPasswordHasher(); 
    } 
} 

一類,然後覆蓋PasswordHasher與繼承PasswordHasher一個新的自定義散列器類。

internal class CustomPasswordHasher : PasswordHasher 
{ 
    public override string HashPassword(string password) 
    { 
     return password; 
     //return Crypto.Sha1.Encrypt(password); 
    } 

    public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword) 
    { 
     //var testHash = Crypto.Sha1.Encrypt(providedPassword); 
     return hashedPassword.Equals(testHash) || hashedPassword.Equals(providedPassword) ? PasswordVerificationResult.Success : PasswordVerificationResult.Failed; 
    } 
} 

最後,請記住,這樣做會導致數據庫用戶的安全性喪失。

+2

告訴某人如何導致密碼破壞不是一個好主意。這相當於向某人解釋如何自殺。是的,他確實問過,那是什麼? –

+0

我瞭解安全。它只需要客戶。 您的代碼有一些問題。沒有看到這個類:IdentityFactoryOptions和IOwinContext。 –

+0

@ DmitryVasilyukJust3F您的客戶已經在黑客名單中。將SO帳戶名稱追溯到公司及其客戶並不困難。如果您堅持禁用密碼安全性,請絕對確保客戶在發生數據泄露時免除責任。是不是一個不安全的密碼重置機制對他們有價值? –

0

由於Asp.NET核心MVC使用依賴注入來設置標識,所有你需要的僅僅是創建密碼哈希類的替代:

public class CustomPasswordHasher : IPasswordHasher<AppUser> 
{ 
    public string HashPassword(AppUser user, string password) 
    { 
     return password; 
    } 

    public PasswordVerificationResult VerifyHashedPassword(AppUser user, string hashedPassword, string providedPassword) 
    { 
     return hashedPassword.Equals(providedPassword) ? PasswordVerificationResult.Success : PasswordVerificationResult.Failed; 
    } 
} 

,並添加:

services.AddScoped<IPasswordHasher<AppUser>, CustomPasswordHasher>(); 

你mvc app statup.cs