向Identity users表添加软删除

本文关键字:删除 添加 Identity users | 更新日期: 2023-09-27 17:50:48

我已经在我的Users表中添加了一个已删除的at列,但显然Identity框架提供的注册新用户方法仍然在数据库中看到这些用户,是否有一种方法告诉它忽略某个列?

// 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 );

…任何其他外部登录的事情都需要告诉这个列

向Identity users表添加软删除

在您的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方法(它在所有"find"方法中被调用):

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;
    }
}