问题描述
是否有一种方法可以获取Windows身份验证用户所在的角色列表,而无需通过 WindowsPrincipal.IsInRole
方法进行显式检查?
Is there a way to get a list of roles a Windows authenticated user is in, without explicitly checking by WindowsPrincipal.IsInRole
method?
推荐答案
WindowsPrincipal.IsInRole
只是检查用户是否是其中的成员具有该名称的组; Windows组是角色。您可以从 WindowsIdentity.Groups
属性中获得用户所属的组的列表。
WindowsPrincipal.IsInRole
just checks if the user is a member of the group with that name; a Windows Group is a Role. You can get a list of the groups that a user is a member of from the WindowsIdentity.Groups
property.
您可以从 WindowsPrincipal
获取 WindowsIdentity
:
WindowsIdentity identity = WindowsPrincipal.Identity as WindowsIdentity;
或者您可以从WindowsIdentity的工厂方法中获取它:
or you can get it from a factory method on WindowsIdentity:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsIdenity.Groups
是 IdentityReference
只是为您提供该组的SID。如果需要组名,则需要将 IdentityReference
转换为 NTAccount
并获取值:
WindowsIdenity.Groups
is a collection of IdentityReference
which just gives you the SID of the group. If you need the group names you will need to translate the IdentityReference
into an NTAccount
and get the Value:
var groupNames = from id in identity.Groups
select id.Translate(typeof(NTAccount)).Value;
这篇关于如何检索用户所属的所有角色(组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!