【问题标题】:asp.net core 2.0 windows role based authorization always returns 403asp.net core 2.0 windows 基于角色的授权总是返回 403
【发布时间】:2018-08-05 20:02:58
【问题描述】:

我正在尝试使用 asp.net core 2.0 应用程序基于 Windows 角色设置基于角色的授权。这是配置:

launchSettings.json:

   {
  "iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:9180/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Bouwfonds.Gems.Onderhoud.Web.UI": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:9181/"
    }
  }
}

StartUp.cs:

  public void ConfigureServices(IServiceCollection services) {
    services.AddMvc();

    services.AddAuthentication(IISDefaults.AuthenticationScheme);

  }

在控制器中:

//管理员的 SID 使用:psgetsid.exe 管理员

  [Authorize(Roles = @"S-1-5-32-544")] 
  public class HomeController : Controller

Windows 身份验证正常,但我总是返回 403。有什么想法吗?

【问题讨论】:

    标签: windows iis asp.net-core asp.net-authorization


    【解决方案1】:

    403 响应通常表示以下两种情况之一:

    • 提供了身份验证,但不允许经过身份验证的用户执行请求的操作。
    • 禁止所有用户操作。例如,当目录列表被禁用时,请求目录列表返回代码 403。

    您确定您在给定的组中吗?首先尝试授权用户,而不是角色:

    [Authorize(Users="Alice, Bob, YourName")]
    

    如果这可行,那么您不是该组的成员,或者该组不存在。

    【讨论】:

    • Microsoft.AspNetCore.Authorization.AuthorizeAttribute 没有 Users 属性,但是我确定我有这个角色。我还尝试了一个新的本地组,所以你可以说“DESKTOP-V3136HA\RoleName”,但结果相同。
    • 你能看看HttpContext.Request.User.Identities看看中间件是否在添加IIS用户吗?
    【解决方案2】:

    本地管理员组作为denyonlysid(?) 声明添加:“{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/denyonlysid: S-1-5-32-544}”所以我想这就是该组不起作用的原因。显然,当您将自己添加到新的本地组时,您必须重新启动才能获得声明:P

    所以它现在可以与本地组一起使用,即使没有 SID 而只是组名。

    【讨论】:

      【解决方案3】:

      这就是我要工作的地方。我使用 Microsoft Authorization Workshop 示例 https://github.com/blowdart/AspNetAuthorizationWorkshop 拼凑了很多内容。虽然我使用的是策略而不是角色。

      Startup.cs

      public void ConfigureServices(IServiceCollection services)
      {
          services.AddAuthorization(options =>
          {
               //Add policies as needed along with authorization classes
               options.AddPolicy("Admin", policy => policy.Requirements.Add(new AdminAuthorization()));
          });
      
          services.AddMvc(config => 
          {
              var policy = new AuthorizationPolicyBuilder()
                  .RequireAuthenticatedUser()
                  .Build();
              config.Filters.Add(new AuthorizeFilter(policy));
          });
      
          //Also add the Authorization Handlers
          services.AddSingleton<IAuthorizationHandler, AdminAuthorization>();
      
          serviecs.AddAuthentication(Microsoft.AspNetCore.Server.IISIntegration.IISDefaults.AuthenticationScheme);
      }
      

      AdminAuthorization.cs

      public class AdminAuthorization : AuthorizationHandler<AdminAuthorization>, IAuthorizationRequirement
      {
           protected override Task HandleRequirementAsync(AuthrizationHandlerContext context, AdminAuthorization  requirement)
           {
               if(context.User.HasClaim(c => c.Value == @"S-1-5-32-544"))
               {
                   context.Succeed(requirement);
               }
               else 
               {
                   context.Fail();
               }
               return Task.CompletedTask;
           }
      }
      

      HomeController.cs

      //Add the name of the policy used in the options.AddPolicy in the startup.cs
      [Authorize(Policy = "Admin")] 
      public class HomeController : Controller
      {
          // your controller logic here
      }
      

      【讨论】:

        猜你喜欢
        • 2020-12-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-27
        • 1970-01-01
        • 2020-02-13
        • 2021-05-23
        • 2018-05-20
        • 2020-05-21
        相关资源
        最近更新 更多