asp.net - VB.NET - 如何使用 Active Directory 将 SID 转换为组名称

标签 asp.net vb.net active-directory usergroups sid

使用 VB.NET,如何将 sid 转换为 Active Directory 组名称?

示例:我需要获取“group_test”而不是“S-1-5-32-544”

我使用的代码是:

Public ReadOnly Property Groups As IdentityReferenceCollection
    Get

        Dim irc As IdentityReferenceCollection
        Dim ir As IdentityReference
        irc = WindowsIdentity.GetCurrent().Groups
        Dim strGroupName As String

        For Each ir In irc
            Dim mktGroup As IdentityReference = ir.Translate(GetType(NTAccount))
            MsgBox(mktGroup.Value)
            Debug.WriteLine(mktGroup.Value)
            strGroupName = mktGroup.Value.ToString

        Next

        Return irc

    End Get
End Property

或者类似的东西?

        currentUser = WindowsIdentity.GetCurrent()

        For Each refGroup As IdentityReference In currentUser.Groups

            Dim acc As NTAccount = TryCast(refGroup.Translate(GetType(NTAccount)), NTAccount)
            If AdminGroupName = acc.Value Then
                ret = "999"
            End If
            If UsersGroupName = acc.Value Then
                ret = "1"
            End If

你将如何使其适应这段代码? (如果用户属于 xx 组,则在下拉列表中显示 xx 组)

        For Each UserGroup In WindowsIdentity.GetCurrent().Groups
            If mktGroup.Value = "BIG" Then
                Dim Company = ac1.Cast(Of MarketingCompany).Where(Function(ac) ac.MarketingCompanyShort = "BIG").FirstOrDefault
                If Company IsNot Nothing Then
                    marketingCo.Items.Add(String.Format("{0} | {1}", Company.MarketingCompanyShort, Company.MarketingCompanyName))
                End If
            End If
        Next

最佳答案

C# 代码:

    public static string GetGroupNameBySid(string sid)
    {
        using(var ctx = 
            new PrincipalContext(ContextType.Domain))
        {
            using(var group = 
                GroupPrincipal.FindByIdentity(
                    ctx, 
                    IdentityType.Sid, 
                    sid))
            {
                return group.SamAccountName;
            }
        }
    }

您必须添加程序集System.DirectoryServices.AccountManagement.dll。 如果您在连接 AD 时遇到任何问题,可以尝试在 PrimaryContext 构造函数中添加 AD 服务器名称。

关于asp.net - VB.NET - 如何使用 Active Directory 将 SID 转换为组名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5857304/

相关文章:

c# - 将变量从 VB 函数传递到客户端 javascript

active-directory - LDAP_MATCHING_RULE_IN_CHAIN 可以返回带有属性的 'subtree search results' (特别是 "memberOf")吗?

azure - 更改企业协议(protocol) Azure 订阅上的默认 Active Directory

c# - 当用户没有将完整的 json 传递给 .NET Web API 时怎么办?

c# - 使用 ASP.Net 标记的 Eval 格式化(仅限)

asp.net - 使用 Visual Studio 2008 和 Windows 7 将 ASP.Net Web 应用程序发布到映射驱动器

javascript - 在查询字符串中使用 "&"和 "+"

vb.net - VB.net使用套接字发送HTTP POST请求

asp.net-mvc - Controller 找不到 View

java - 使用 JNDI/Java 中的当前用户在 LDAP 上进行身份验证