Active Directory - 用户的角色
- 作者: 缘生缘灭缘如水
- 来源: 51数据库
- 2022-10-21
问题描述
我了解如何使用 User.Identity 和 User.IsInRole
I understand how to use User.Identity and User.IsInRole
有没有办法查看用户的所有角色?
Is there a way to see all of the roles a user is in?
我们有很多组,有些人在很多组中,但我不想写 User.IsInRole 20+ 次.
We have a lot of groups and some people are in a lot of groups, but I don't want to write a User.IsInRole 20+ times.
推荐答案
在 Active Directory 上下文中,您所指的角色实际上是用户所属的安全(或授权)组.
In an Active Directory context, the Roles you refer to are really the security (or authorization) groups a user is a member of.
因此,如果您使用 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement (S.DS.AM) 命名空间.在此处阅读所有相关信息:
So if you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:
- 在 .NET Framework 3.5 中管理目录安全主体莉>
- 有关 System.DirectoryServices.AccountManagement 的 MSDN 文档
基本上,您可以定义域上下文并轻松找到 AD 中的用户和/或组:
Basically, you can define a domain context and easily find users and/or groups in 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 中与用户和组一起玩变得非常容易!
The new S.DS.AM makes it really easy to play around with users and groups in AD!
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
