【发布时间】:2013-05-10 20:20:57
【问题描述】:
我了解如何使用User.Identity 和User.IsInRole
有没有办法查看用户的所有角色?
我们有很多组,有些人在很多组中,但我不想写 User.IsInRole 20 多次。
【问题讨论】:
标签: c# active-directory
我了解如何使用User.Identity 和User.IsInRole
有没有办法查看用户的所有角色?
我们有很多组,有些人在很多组中,但我不想写 User.IsInRole 20 多次。
【问题讨论】:
标签: c# active-directory
在 Active Directory 上下文中,您所指的角色实际上是用户所属的安全(或授权)组。
因此,如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间。在此处阅读所有相关信息:
基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// get the authorization groups - those are the "roles"
var groups = user.GetAuthorizationGroups();
foreach(Principal principal in groups)
{
// do something with the group (or role) in question
}
}
}
新的 S.DS.AM 让在 AD 中与用户和组一起玩变得非常容易!
【讨论】: