2011-05-02 52 views
5

使用VB.NET,如何使用Active Directory將sid轉換爲組名稱?VB.NET - 如何使用Active Directory將SID轉換爲組名稱

例如:我需要得到 「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 

回答

2

下面是一個簡單的方法在C#writen,我認爲它不是很難適應:

/* 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(); 

這是在VB .NET感謝REFLECTOR

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

給出
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 
+0

@JPBlanc,謝謝你的回覆。你能解釋一下這段代碼的作用嗎?我是否需要分別對每個SID進行硬編碼? – 2011-05-02 20:57:35

+0

對不起,我不明白你的問題。此代碼基於Active-Directory將SID轉換爲組名。你只需要在它的頂部創建一個字符串作爲參數的方法。 – JPBlanc 2011-05-03 03:19:50

+0

@JBBlanc,你在哪裏得到值「S-1-5-21-3115856885-816991240-3296679909-1106」? – 2011-05-03 12:40:49

3

這裏是一個鏈接,如何將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

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從ASP.NET使用登錄的Developer Fusion's Converter Tool組成員:

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 
+0

感謝您的回覆。你可以發佈一些示例代碼? – 2011-05-02 20:58:00

7

代碼在C#:

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

您必須添加組件System.DirectoryServices.AccountManagement.dll。 如果您在連接到AD時遇到任何問題,您可以嘗試在PrincipalContext構造函數中添加AD服務器名稱。

+0

感謝您的回覆!你的意思是我應該添加「Imports System.DirectoryServices.AccountManagement」到Default.aspx.vb嗎?我在哪裏聲明PrincipalContext構造函數?我正在查看MSDN,網址爲http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principal.aspx#Y40,http://msdn.microsoft.com/en-us/library /system.directoryservices.accountmanagement.principalcontext.aspx以及下列方法:GetGroups,GetGroups(PrincipalContext),IsMemberOf(PrincipalContext,IdentityType,String),IsMemberOf(GroupPrincipal)。 – 2011-05-05 12:44:56

+0

@ brian-mccarthy右鍵單擊項目的參考,然後單擊「添加參考...」。在.NET選項卡中找到「System.DirectoryServices.AccountManagement」組件並雙擊它或按確定。您不需要聲明PrincipalContext構造函數,因爲此類已在上面添加的程序集中定義。我在例子中使用了一個參數構造函數。如果您在連接到AD時遇到問題(我沒有,但是如果您的AD客戶端(即您的應用程序)不在域中可能會發生此問題),則可以使用PrincipalContext的兩個參數構造函數並將AD服務器主機名作爲第二個論點。 – meir 2011-05-11 09:07:53

+0

Briliantly!正是我所期待的 – Vlad 2015-10-28 18:18:54

相關問題