ASP.NET Core Authorize AD Groups through web.config

前端 未结 2 1275
长情又很酷
长情又很酷 2020-12-13 16:34

In my old .NET MVC app, I could enable Windows Authentication in IIS and disable anonymous. Then in my web.config file I just had to put in this:



        
2条回答
  •  孤城傲影
    2020-12-13 17:10

    I solved this by making it into a policy which is able to call appsettings.json. This way other people who have access to the server can then edit the group to their own.

    In Startup.cs:

    services.AddAuthorization(options =>
    {
        options.AddPolicy("ADRoleOnly", policy => policy.RequireRole(Configuration["SecuritySettings:ADGroup"]));
    });
    
    services.AddMvc(config =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
    
        config.Filters.Add(new AuthorizeFilter(policy));
    });
    

    In appsettings.json (or perhaps appsettings.production.json if you have different):

    "SecuritySettings": {
      "ADGroup": "YourDomain\\YourADGroup"
    }
    

    In your controllers you can then decorate it with this attribute:

    [Authorize(Policy = "ADRoleOnly")]
    

    Hope this can help other people

    I have still to figure out how to apply this policy globally, so I don't have to authorize every controller, I'd figure it can be done in the services.AddMvc somehow?

提交回复
热议问题