我明白如何使用User.Identity
and User.IsInRole
有没有办法查看用户所处的所有角色?
我们有很多组,有些人在很多组中,但我不想编写 User.IsInRole 20 多次。
在 Active Directory 上下文中,Roles您所指的实际上是用户所属的安全(或授权)组。
因此,如果您使用的是 .NET 3.5 及更高版本,您应该查看System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关内容:
- 管理 .NET Framework 3.5 中的目录安全主体
- 关于 System.DirectoryServices.AccountManagement 的 MSDN 文档
基本上,您可以定义域上下文并轻松在 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 中的用户和组的使用变得非常容易!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)