重新检查授权用户(asp.net核心)

Re-check authorized user (asp.net core)

我是 asp.net 核心剃须刀页面的新手。

几天后,我可以使用我自己的数据库和代码实现身份验证过程。

我的登录页面会检查 user/password 以及用户是否处于活动状态(我的 table 中的标志)。

假设管理员更改了授权用户(他正在使用系统),将此用户设置为非活动状态,甚至更改授权角色。如何重新检查管道中的授权?

我的代码

    public void ConfigureServices(IServiceCollection services)
    {
....
      services.AddScoped<IAuthenticateService, AuthenticateService>();

      services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
         .AddCookie(options =>
         {
           options.LoginPath = "/Login";
           options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
           options.AccessDeniedPath = "/NotAcess";
         });
......

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
....
      app.UseAuthentication();
      app.UseAuthorization();

    // In AuthenticateService (called by login page after checking user existence and password)
    private async Task AuthenticateExecAsync(User user, HttpContext httpContext)
    {
      if (user.Status != User.Status_Active)
        throw new Exception("User inactive");

      var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
      identity.AddClaim(new Claim(ClaimTypes.Name, user.Email));
      identity.AddClaim(new Claim(ClaimTypes.GivenName, user.Name));
      List<string> roles = new List<string>();
      foreach (var item in user.userRoles)
        roles.Add(item.Role.RoleId);
      foreach (var role in roles)
        identity.AddClaim(new Claim(ClaimTypes.Role, role));
      var principal = new ClaimsPrincipal(identity);
      await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

谢谢

也许您将 IAuthenticationService 注入到需要验证的 class 中?

本论坛解决的问题:

https://forums.asp.net/p/2167309/6306278.aspx?p=True&t=637261738568923978