Adding soft delete to Identity users table

社会主义新天地 提交于 2019-12-08 02:23:33

问题


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?

Registration

// this needs to ignore any DeletedAt where not null
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
    ...
}

Logging in

// 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.


回答1:


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
         {
             ...
         }                      
     }
}

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


来源:https://stackoverflow.com/questions/30458914/adding-soft-delete-to-identity-users-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!