C# 存储未实现IUserPasswordStore<;TUser>;ASP.NET标识
在visual studio为Web Api 2的ASP.NET身份验证项目生成的模板项目中,我添加了一个类ApplicationUserStore(这是我的自定义userstore)C# 存储未实现IUserPasswordStore<;TUser>;ASP.NET标识,c#,asp.net,asp.net-web-api2,asp.net-mvc-5.1,C#,Asp.net,Asp.net Web Api2,Asp.net Mvc 5.1,在visual studio为Web Api 2的ASP.NET身份验证项目生成的模板项目中,我添加了一个类ApplicationUserStore(这是我的自定义userstore) 公共类ApplicationUserStore:IUserStore { 私有只读应用程序的bContext\u上下文; 公共ApplicationUserStore(ApplicationDbContext上下文) { _上下文=上下文; } 公共异步任务FindByIdAsync(字符串用户名) { 返回wa
公共类ApplicationUserStore:IUserStore
{
私有只读应用程序的bContext\u上下文;
公共ApplicationUserStore(ApplicationDbContext上下文)
{
_上下文=上下文;
}
公共异步任务FindByIdAsync(字符串用户名)
{
返回wait _context.Users.Include(x=>x.FMBP_Company).FirstOrDefaultAsync(n=>n.UserName==UserName);
}
公共任务CreateAsync(ApplicationUser用户)
{
抛出新的NotImplementedException();
}
公共任务DeleteAsync(ApplicationUser用户)
{
抛出新的NotImplementedException();
}
公共任务FindByNameAsync(字符串用户名)
{
返回_context.Users.Include(x=>x.FMBP_Company).FirstOrDefaultAsync(n=>n.UserName==UserName);
}
公共任务更新同步(ApplicationUser用户)
{
抛出新的NotImplementedException();
}
公共空间处置()
{
抛出新的NotImplementedException();
}
}
我之所以这样做,基本上是因为每当我查找dbyidasync时,我都需要FMBP_Company对象
我甚至更改了IdentiyConfig.cs并添加了以下行,以便可以使用我的自定义userstore
var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>()));
var-manager=newapplicationUserManager(newapplicationUserStore(context.Get());
一旦我这样做了,我在注册时就出现了一个错误
存储未实现IUserPasswordStore
回答你的问题。
在身份验证过程中,除非您实现了一些非常特殊的东西,否则您需要实现多个接口,因为一旦您控制了管理器和存储库,您就必须提供代码来处理多个事情。
即使您不想实现自定义密码散列,也必须实现该接口,因为管理器已被覆盖,并且没有更多的密码处理实现
如果不想重写哈希过程的任何内容,请实现IUserPasswordStore并简单地编写:
public Task SetPasswordHashAsync(YourUser user, string passwordHash)
{
user.PasswordHash = passwordHash;
return Task.FromResult(0);
}
public Task<string> GetPasswordHashAsync(YourUser user)
{
return Task.FromResult<string>(user.PasswordHash);
}
public Task<bool> HasPasswordAsync(YourUser user)
{
return Task.FromResult<bool>(!String.IsNullOrEmpty(user.Password));
}
public任务SetPasswordHashAsync(您的用户,字符串passwordHash)
{
user.PasswordHash=PasswordHash;
返回Task.FromResult(0);
}
公共任务GetPasswordHashAsync(您的用户)
{
返回Task.FromResult(user.PasswordHash);
}
公共任务HasPasswordAsync(您的用户)
{
返回Task.FromResult(!String.IsNullOrEmpty(user.Password));
}
其中YourUser通常是IUser的当前用户(当然可能是继承的类)
看看代码,它非常简单,只需为对象分配哈希密码,返回对象中的内容和检查用户是否有密码的布尔值。不要忘记在
UserStore.cs
类中实现IUserPasswordStore
接口
例如:
public class UserStore :
IUserStore<MyIdentityUser>,
IUserPasswordStore<MyIdentityUser>,
IUserRoleStore<MyIdentityUser>
{
...
}
公共类用户存储:
Iuser商店,
IUserPasswordStore,
IUserRoleStore
{
...
}
那么您是否更新了ApplicationUserStore
以实现IUserPasswordStore
接口?然后,您将拥有公共类ApplicationUserStore:IUserStore,IUserPasswordStore
@BrendanGreen是的,我现在已经实现了这个接口,但我不知道在这里实现什么,因为我不需要自定义密码代码,但解决了我的问题
public class UserStore :
IUserStore<MyIdentityUser>,
IUserPasswordStore<MyIdentityUser>,
IUserRoleStore<MyIdentityUser>
{
...
}