使用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
@JPBlanc,謝謝你的回覆。你能解釋一下這段代碼的作用嗎?我是否需要分別對每個SID進行硬編碼? – 2011-05-02 20:57:35
對不起,我不明白你的問題。此代碼基於Active-Directory將SID轉換爲組名。你只需要在它的頂部創建一個字符串作爲參數的方法。 – JPBlanc 2011-05-03 03:19:50
@JBBlanc,你在哪裏得到值「S-1-5-21-3115856885-816991240-3296679909-1106」? – 2011-05-03 12:40:49