ASP.NET角色如何与授权一起使用?

问题描述 投票:0回答:1

我正在使用ASP.NET Core并托管启用Windows身份验证的基本默认模板。我将其托管在专用的IIS服务器上,并已验证该应用程序正在从AD接收正确的信息,并且它可以正确地验证我的会话。

我觉得我正在尝试做一些非常简单的事情。如果用户位于安全组中(来自AD)“管理员”,则他们可以访问特定功能。如果他们不在该组中,则无法访问。

我在服务的[Authorize]属性上拍了一下

(in ConfigureServices)
services.AddAuthentication(IISDefaults.AuthenticationScheme);
(in Configure)
app.UseAuthorization();

(in service)
[Authorize]
public class SiteService
    {
        private readonly string _route;
        private readonly HttpClient _httpClient;

        public SiteService(HttpClient httpClient)
        {
            _httpClient = httpClient;
            _route = httpClient.BaseAddress.AbsoluteUri;
        }

        public async Task<IEnumerable<Site>> GetSites()
        {

        }
   }

我可以在日志中看到访问服务给了我域/用户。然后,我在此处查找了MS Docs:https://docs.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-3.1

并在[Authorize(Roles =“ Admin”)上打了耳光。那行得通。然后,我用“ sldkfjslksdlfkj”切换了“管理员”。没改变...我仍然可以访问该服务。

为什么Roles =“ x”检查不起作用?如何为安全组启用相对简单的AD支票检查?

asp.net windows-authentication asp.net-core-3.0
1个回答
0
投票

您可以编写custom Policy Authorization handlers来检查所有用户的ADGroup,并检查它们是否包含所需的组名。

请参阅以下内容:

1。创建CheckADGroupRequirement(接受参数)

    public class CheckADGroupRequirement : IAuthorizationRequirement
    {
        public string GroupName { get; private set; }

        public CheckADGroupRequirement(string groupName)
        {
            GroupName = groupName;
        }
    }

2。创建CheckADGroupHandler

    public class CheckADGroupHandler : AuthorizationHandler<CheckADGroupRequirement>
    {
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
                                                       CheckADGroupRequirement requirement)
        {
            //var isAuthorized = context.User.IsInRole(requirement.GroupName);

            var groups = new List<string>();//save all your groups' name
            var wi = (WindowsIdentity)context.User.Identity;
            if (wi.Groups != null)
            {
                foreach (var group in wi.Groups)
                {
                    try
                    {
                        groups.Add(group.Translate(typeof(NTAccount)).ToString());
                    }
                    catch (Exception e)
                    {
                        // ignored
                    }
                }
               if(groups.Contains(requirement.GroupName))//do the check
                {
                    context.Succeed(requirement);
                }
            }

            return Task.CompletedTask;
        }
    }

3.ConfigureServices中的注册处理程序

    services.AddAuthorization(options =>
    {
        options.AddPolicy("AdminOnly", policy =>
            policy.Requirements.Add(new CheckADGroupRequirement("DOMAIN\\Domain Admin")));//set your desired group name
            //other policies
    });

    services.AddSingleton<IAuthorizationHandler, CheckADGroupHandler>();

4。在控制器/服务上使用

    [Authorize(Policy = "AdminOnly")]
    public class SiteService
© www.soinside.com 2019 - 2024. All rights reserved.