Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET角色如何与授权一起使用?_Asp.net_Windows Authentication_Asp.net Core 3.0 - Fatal编程技术网

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

ASP.NET角色如何与授权一起使用?,asp.net,windows-authentication,asp.net-core-3.0,Asp.net,Windows Authentication,Asp.net Core 3.0,我正在使用ASP.NET Core并托管基本上是默认的模板,并且启用了Windows身份验证。我在一个专用的IIS服务器上托管这个应用程序,并已验证该应用程序是否从AD接收到正确的信息,以及它是否正确验证了我的会话 我觉得我在尝试做一些非常简单的事情。如果用户属于安全组(来自AD)“Admin”,则他们可以访问特定功能。如果他们不在该组中,则无法访问 我给服务加上了[Authorize]属性 (in ConfigureServices) services.AddAuthentication(II

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

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

我给服务加上了[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()
        {

        }
   }
(在配置服务中)
服务.AddAuthentication(IISDefaults.AuthenticationScheme);
(在配置中)
app.UseAuthorization();
(在职)
[授权]
公共类站点服务
{
私有只读字符串_路由;
私有只读HttpClientu HttpClient;
公共站点服务(HttpClient HttpClient)
{
_httpClient=httpClient;
_route=httpClient.BaseAddress.AbsoluteUri;
}
公共异步任务GetSites()
{
}
}
我可以在日志中看到,访问服务给我提供了域/用户。然后我在这里查阅了MS文档:

然后,我将“Admin”改为“sldkfjslksdlfkj”。没有任何变化……我仍然可以访问该服务

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

您可以编写一个脚本来检查所有用户的AD组,并检查它们是否包含所需的组名

请参阅以下内容:

1.创建CheckADGroupRequirement(接受参数)

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

创建自定义授权属性,如下所示:
    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;
        }
    }
    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>();
    [Authorize(Policy = "AdminOnly")]
    public class SiteService