使用 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/