将软删除添加到“身份用户"表 [英] Adding soft delete to Identity users table

查看:133
本文介绍了将软删除添加到“身份用户"表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've added a deleted at column to my Users table but obviously registering new users method provided by Identity framework still sees these users in the database, is there a way of telling it to ignore a certain column?

注册

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

...以及有关该列的所有其他外部登录事件都需要告知.

...And any other external login things going on need to be told about the column.

推荐答案

在您的signInManager覆盖SignInAsync方法中,此方法用于每个登录过程(本地或外部)

Inside your signInManager overwrite the SignInAsync method, this one is used in every login proccess (local or external)

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方法(在所有查找"方法内部调用):

Or create a custom UserStore and override GetUserAggregateAsync method (its called inside all "find" methods):

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

这篇关于将软删除添加到“身份用户"表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆