2016-01-22 48 views
8

我有一个asp.net5项目设置使用Windows身份验证。当我设置一个断点并查看用户时,我看到有一个包含组SID的声明数组。如何从索赔中获得实际的组名称?AspNet5 - Windows身份验证从声明获取组名称

我想限制用户使用他们属于的活动目录组登录的窗口,并且正在努力设置它。

问题: 如何查看登录用户所属的活动目录组? 如何将GroupSID转换为组名? 我是否需要在startup.cs中包含任何内容以将某些组限制到REST服务调用?

我看到基于登录用户手动设置声明的示例。我有兴趣使用Windows身份验证用户及其组来限制访问权限。

谢谢

回答

3

你没有。不幸的是,这不是Windows身份验证如何工作。你只能检查一个用户是否在一个角色中(并且有一个策略要求),而不是枚举他们所在的角色 - 这需要目录服务并且没有被移植到核心。

(有一点需要注意的是,呃,User.IsInRole()被打破,现在的Windows身份将被固定在RC2)

+0

感谢您的回答。我现在可以拉下RC2吗?还是RC2不可用?它是否像asp.net 4一样简单,我用[Authenticate]和[Authorize(“groupname”)]装饰休息方法,还是必须编写一堆自定义代码? –

+0

你还不明白。不久。一旦它起作用,是的,如果你不想使用策略,情况会一样。你可以简单地做[授权(角色='')] – blowdart

+0

@blowdart你有任何机会得到github问题的方便,为什么IsInRole()被破坏,或碰巧知道它是如何被破坏? – JosephGarrone

11

使用以下实际上,你可以仍然获得组名称:

var test = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3290390516-4063083420-3538132138-1146").Translate(typeof(System.Security.Principal.NTAccount)).ToString(); 

因此,例如:

var roles = ((ClaimsIdentity)_context.User.Identity).Claims.Where(q => q.Type == ClaimTypes.GroupSid).Select(q => q.Value); 

_logger.LogInformation($"Got {roles.Count()} roles"); 

foreach (var role in roles) 
{ 
    var name = new System.Security.Principal.SecurityIdentifier(role).Translate(typeof(System.Security.Principal.NTAccount)).ToString(); 
    _logger.LogInformation($"Got role {role}"); 
} 

输出:

(namespace).Authorization.Handlers.SiteHandler: Information: Got 18 roles 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\Domain Users 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role Everyone 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\(redacted) Backend 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\(redacted) Dashboards 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role BUILTIN\Performance Log Users 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role BUILTIN\Users 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role NT AUTHORITY\INTERACTIVE 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role CONSOLE LOGON 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role NT AUTHORITY\Authenticated Users 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role NT AUTHORITY\This Organization 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role LOCAL 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\jira-users 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\jira-developers 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\(redacted)_PDMS_DE_ALL 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\(redacted)_PDMS_BE_ALL 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\(redacted)Developers 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\(redacted)_TEST 
(namespace).Authorization.Handlers.SiteHandler: Information: Got role (redacted)\(redacted)_PDMS_DB_ALL 

请注意,域角色可能需要一两秒钟才能填充。

+0

这真的很有用,直到RC2发布。好的一个 –

+3

现在有1.0更好的方法吗? – Charles