问题
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