2016-01-22 34 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

相關問題