gpt4 book ai didi

c# - 在没有 IUserPasswordStore 的情况下实现 ASP.NET Identity IUserLockoutStore

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:26 26 4
gpt4 key购买 nike

我有 MVC5 项目,我没有直接访问用户数据库的权限,而是为我提供了 Web 服务,例如:

bool register (string username, string password, string email) //password is plain text
bool login (string username, string password)

我的问题是我不知道如何实现 IUserPasswordStore,因为 ASP Identity 强制我使用 Hash 而我不允许存储散列密码,因为这个 Web 服务将不仅仅由我使用。

我的 UserStore.cs:

public Task<string> GetPasswordHashAsync(TUser user)
{
//I'm suppose to provide hashed password here, but since I don't have any hashed password stored, I have no idea how to implement this method
string passwordHash = userTable.GetPassword(user.Id);

return Task.FromResult<string>(passwordHash);
}

public Task<bool> HasPasswordAsync(TUser user)
{
var hasPassword = !string.IsNullOrEmpty(userTable.GetPassword(user.Id));

return Task.FromResult<bool>(Boolean.Parse(hasPassword.ToString()));
}

public Task SetPasswordHashAsync(TUser user, string passwordHash)
{
//do nothing since I don't need to hash the password

return Task.FromResult<Object>(null);
}

并且我被告知不要因此实现 IUserPasswordStore,而只是在 SignInManager 中使用 SignInAsync 或覆盖 PasswordSignInAsync,这工作正常但是我遇到了另一个问题,如果他们多次登录尝试失败我需要锁定用户并且它不实现 IUserPasswordStore 就不可能实现 IUserLockoutStore。

最佳答案

找到答案:我创建了一个新类来实现 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;
}
}

...并在IdentityConfig中注册:

manager.PasswordHasher = new CustomPasswordHasher();

关于c# - 在没有 IUserPasswordStore 的情况下实现 ASP.NET Identity IUserLockoutStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com