使用 VB.NET 检测 Windows 用户所在的域用户组

发布于 2024-08-04 07:05:36 字数 118 浏览 3 评论 0原文

我知道使用 Dim currUser As String = Request.ServerVariables("LOGON_USER") 返回域\用户名,但我想知道该用户在 Active Directory 中属于哪个组。

I know that using Dim currUser As String = Request.ServerVariables("LOGON_USER") returns the Domain\Username, but I want to know what Group that user is in say in Active Directory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林迷了鹿 2024-08-11 07:05:36

您想要群组列表吗?或者您想检查用户是否是特定组的成员?

如果是后者,您可以使用 WindowsPrincipal.IsInRole() 检查用户是否属于特定组:

http://msdn.microsoft.com/en-us/library/fs485fwh.aspx

例如,如果您想检查用户是否是管理员,您可以使用:

If Page.User.IsInRole("BUILTIN\Administrators") Then
    ' Do something
End If

Do you want a list of the groups? Or do you want to check if the user is a member of a specific group?

If the latter, you can use WindowsPrincipal.IsInRole() to check if the user belongs to a specific group:

http://msdn.microsoft.com/en-us/library/fs485fwh.aspx

For example, if you want to check if the user is an Administrator you can use:

If Page.User.IsInRole("BUILTIN\Administrators") Then
    ' Do something
End If
想你只要分分秒秒 2024-08-11 07:05:36

您可以使用 UserPrincipal.GetAuthorizationGroups 方法

imports System.DirectoryServices.AccountManagement
dim name as string = Request.ServerVariables("LOGON_USER") 
dim user As UserPrincipal = UserPrincipal.FindByIdentity( new PrincipalContext( ContextType.Domain ), name)
dim groups As PrincipalSearchResult(Of Principal)= user.GetAuthorizationGroups()

You can use UserPrincipal.GetAuthorizationGroups Method

imports System.DirectoryServices.AccountManagement
dim name as string = Request.ServerVariables("LOGON_USER") 
dim user As UserPrincipal = UserPrincipal.FindByIdentity( new PrincipalContext( ContextType.Domain ), name)
dim groups As PrincipalSearchResult(Of Principal)= user.GetAuthorizationGroups()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文