我已经在我的用户表中添加了一个已删除的列,但显然身份框架提供的注册新用户方法仍然在数据库中看到这些用户,有没有办法告诉它忽略某个列?
报名
// this needs to ignore any DeletedAt where not null
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
...
}
正在登录
// this needs to ignore any DeletedAt where not null
result = await SignInManager.PasswordSignInAsync( user.UserName, model.Password, model.RememberMe, shouldLockout: true );
...任何其他外部登录发生的事情都需要告知该专栏。
在你的
signInManager
中覆盖SignInAsync
方法,这个方法在每个登录过程(本地或外部)中使用
public class ApplicationSignInManager : SignInManager<User, int>
{
public override async Task SignInAsync(User user, bool isPersistent, bool rememberBrowser)
{
if (!user.isDeleted)
{
await base.SignInAsync(user, isPersistent, rememberBrowser);
}
else
{
...
}
}
}
或者创建自定义
UserStore
并覆盖 GetUserAggregateAsync
方法(在所有“查找”方法中调用):
public class CustomUserStore : UserStore<ApplicationUser>
{
public CustomUserStore(ApplicationDbContext context) : base(context) { }
protected override async Task<ApplicationUser> GetUserAggregateAsync(Expression<Func<ApplicationUser, bool>> filter)
{
var user = await base.GetUserAggregateAsync(filter);
// if user is found but soft deleted then ignore and return null
if (user != null && user.IsDeleted)
{
return null;
}
return user;
}
}
您可以覆盖基本 UserStore 中的
Users
属性
public class CustomUserStore : UserStore<ApplicationUser,...>
{
public CustomUserStore(ApplicationDbContext context) : base(context) { }
public override IQueryable<ApplicationUser> Users => base.Users.Where(u => !u.Deleted);
}