VB.NET-アクティブディレクトリでSIDをグループ名に変換する方法
-
27-10-2019 - |
質問
VB.NETを使用して、SIDをActive Directoryでグループ名にどのように変換しますか?
例:「S-1-5-32-544」ではなく「group_test」を取得する必要があります
私が使用しているコードは次のとおりです。
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#の単純な方法は次のとおりです。適応するのは難しくないと思います。
/* 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にあります リフレクター
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
-----編集-----そうですがあなたが望むものはここにありますが、それは以前に@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
他のヒント
C#のコード:
public static string GetGroupNameBySid(string sid)
{
using(var ctx =
new PrincipalContext(ContextType.Domain))
{
using(avr group =
GroupPrincipal.FindByIdentity(
ctx,
IdentityType.Sid,
sid))
{
return group?.SamAccountName;
}
}
}
Assembly System.directoryServices.accountmanagement.dllを追加する必要があります。 ADに接続して問題がある場合は、starpinepContextコンストラクターにADサーバー名を追加してみてください。
SIDを名前に変換する方法のリンクは次のとおりです。 http://vbdotnet.canbal.com/view.php?sessionid=jef85k%2b%2bebj9pz%2bwz9hjjicw%2fyeptadxfcpycovz7js%3d
基本的に、DirectoryEntryオブジェクトを取得して、名前を取得するために使用できます。ただし、私がこれを行う簡単な方法であると私が信じているものを探している場合は、現在のユーザーを連れてGroupメンバーシップを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からログインしたユーザーのユーザーグループメンバーシップを取得します 開発者Fusionのコンバーターツール:
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