gpt4 book ai didi

c# - 向身份用户表添加软删除

转载 作者:行者123 更新时间:2023-11-30 21:57:52 25 4
gpt4 key购买 nike

我已经在我的用户表中添加了一个 deleted at 列,但显然身份框架提供的注册新用户方法仍然在数据库中看到这些用户,有没有办法告诉它忽略特定列?

注册

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

关于c# - 向身份用户表添加软删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30458914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com