mur*_*tgu 24 .net rbac windows-principal windows-identity
有没有办法获取Windows身份验证用户所在的角色列表,而无需通过WindowsPrincipal.IsInRole方法明确检查?
jos*_*rry 39
WindowsPrincipal.IsInRole只检查用户是否是具有该名称的组的成员; Windows组是一个角色.您可以从WindowsIdentity.Groups属性中获取用户所属的组列表.
你可以WindowsIdentity从你的WindowsPrincipal:
WindowsIdentity identity = WindowsPrincipal.Identity as WindowsIdentity;
Run Code Online (Sandbox Code Playgroud)
或者您可以从WindowsIdentity上的工厂方法获取它:
WindowsIdentity identity = WindowsIdentity.GetCurrent();
Run Code Online (Sandbox Code Playgroud)
WindowsIdenity.Groups是一个集合IdentityReference,它只给你组的SID.如果您需要组名,则需要将其转换IdentityReference为a NTAccount并获取值:
var groupNames = from id in identity.Groups
select id.Translate(typeof(NTAccount)).Value;
Run Code Online (Sandbox Code Playgroud)
编辑:乔希打败了我!:)
试试这个
using System;
using System.Security.Principal;
namespace ConsoleApplication5
{
internal class Program
{
private static void Main(string[] args)
{
var identity = WindowsIdentity.GetCurrent();
foreach (var groupId in identity.Groups)
{
var group = groupId.Translate(typeof (NTAccount));
Console.WriteLine(group);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
如果未连接到域服务器,该Translate函数可能会抛出以下异常The trust relationship between this workstation and the primary domain failed.
但是对于大多数人来说,它会没问题,所以我使用:
foreach(var s in WindowsIdentity.GetCurrent().Groups) {
try {
IdentityReference grp = s.Translate(typeof (NTAccount));
groups.Add(grp.Value);
}
catch(Exception) { }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19262 次 |
| 最近记录: |