首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VB.NET -如何使用Active Directory将SID转换为组名

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

Stack Overflow用户
提问于 2011-05-02 20:48:54
回答 3查看 12.2K关注 0票数 5

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

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

我使用的代码是:

代码语言:javascript
复制
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

或者像这样的东西?

代码语言:javascript
复制
        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组)

代码语言:javascript
复制
        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
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-03 03:15:15

这是一个用C#编写的简单方法,我认为它不难适应:

代码语言:javascript
复制
  /* Retreiving object from SID
  */
  string SidLDAPURLForm = "LDAP://WM2008R2ENT:389/<SID={0}>";
  System.Security.Principal.SecurityIdentifier sidToFind = new System.Security.Principal.SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106");

  DirectoryEntry userEntry = new DirectoryEntry(string.Format(SidLDAPURLForm, sidToFind.Value));

  string name = userEntry.Properties["cn"].Value.ToString();

多亏了REFLECTOR,它在VB .NET中实现了

代码语言:javascript
复制
Dim SidLDAPURLForm As String = "LDAP://WM2008R2ENT:389/<SID={0}>"
Dim sidToFind As New SecurityIdentifier("S-1-5-21-3115856885-816991240-3296679909-1106")
Dim userEntry As New DirectoryEntry(String.Format(SidLDAPURLForm, sidToFind.Value))
Dim name As String = userEntry.Properties.Item("cn").Value.ToString

- EDITED -这是你想要的,但它与之前@BiggsTRC提供的是一样的

代码语言:javascript
复制
Private Shared Sub Main(args As String())
    Dim currentUser As WindowsIdentity = WindowsIdentity.GetCurrent()

For Each iRef As IdentityReference In currentUser.Groups
        Console.WriteLine(iRef.Translate(GetType(NTAccount)))
    Next
End Sub
票数 2
EN

Stack Overflow用户

发布于 2011-05-05 14:31:49

C#中的代码:

代码语言:javascript
复制
    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时遇到任何问题,您可以尝试在PrincipalContext构造函数中添加AD服务器名称。

票数 9
EN

Stack Overflow用户

发布于 2011-05-03 00:32:24

以下是如何将SID转换为名称的链接:http://vbdotnet.canbal.com/view.php?sessionid=JEf85K%2B%2BeBj9Pz%2BWz9hJJicW%2FYEPtADXfcpYCovZ7js%3D

基本上,您会得到一个DirectoryEntry对象,然后可以使用它来获取名称。但是,如果您正在寻找我认为更简单的方法来完成此操作,只需在AD中查找当前用户并查找其组成员身份。以下是如何做到这一点的示例(您将需要更大的文章来实际完成您的任务,但此代码是对您的问题的具体答案):http://www.codeproject.com/KB/system/everythingInAD.aspx#39

很抱歉代码是用C#编写的。但是,您应该能够只使用转换器将其转换为VB.NET,这是没有问题的。

从C#中的ASP.NET获取已登录用户的用户组成员身份

代码语言:javascript
复制
public ArrayList Groups()
{
    ArrayList groups = new ArrayList();

    foreach (System.Security.Principal.IdentityReference group in
            System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups)
    {
        groups.Add(group.Translate(typeof
        (System.Security.Principal.NTAccount)).ToString());
    }

    return groups;
 }

在VB.NET中使用Developer Fusion's Converter Tool从ASP.NET获取已登录用户的用户组成员身份:

代码语言:javascript
复制
    Public Function Groups() As ArrayList
        Dim groups__1 As New ArrayList()

        For Each group As System.Security.Principal.IdentityReference In                 System.Web.HttpContext.Current.Request.LogonUserIdentity.Groups

               groups__1.Add(group.Translate(GetType(System.Security.Principal.NTAccount)).ToString())
        Next

    Return groups__1
    End Function
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5857304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档