トップ回答者
ログオンユーザーの所属するセキュリティグループの取得

質問
回答
-
VS .NET2003で作ったものなので ADSI を使ってるため2005では名前空間が違うと思いますが、
ActiveDirectory ユーザの DirectoryEntry が取得できていて、これが引数 Entry に設定されているとすると
'グループ名のリストを取得するメソッド
Private Function GetGroupList(ByVal Entry As DirectoryEntry) As Specialized.StringCollection
Dim User As ActiveDs.IADsUser = DirectCast(Entry.NativeObject, ActiveDs.IADsUser)
Dim Groups() As Object = DirectCast(User.GetEx("MemberOf"), Object())
Dim GroupList As New Specialized.StringCollection
Dim GroupName As String
For Each grp As Object In Groups
GroupName = LDAPPathToGroupName(grp.ToString())
GroupList.Add(GroupName)
Next
Return GroupList
End Function
'グループ名のLDAPパスからグループ名部分を抜き出すメソッド
Private Function LDAPPathToGroupName(ByVal LDAPPath As String) As String
With LDAPPath
Dim spos As Integer = .IndexOf("="c) + 1
Dim epos As Integer = .IndexOf(","c)
If epos > 0 Then
Return .Substring(spos, epos - spos)
Else
Return .Substring(spos)
End If
End With
End Function
これでグループ名のリストを取得できます。
すべての返信
-
VS .NET2003で作ったものなので ADSI を使ってるため2005では名前空間が違うと思いますが、
ActiveDirectory ユーザの DirectoryEntry が取得できていて、これが引数 Entry に設定されているとすると
'グループ名のリストを取得するメソッド
Private Function GetGroupList(ByVal Entry As DirectoryEntry) As Specialized.StringCollection
Dim User As ActiveDs.IADsUser = DirectCast(Entry.NativeObject, ActiveDs.IADsUser)
Dim Groups() As Object = DirectCast(User.GetEx("MemberOf"), Object())
Dim GroupList As New Specialized.StringCollection
Dim GroupName As String
For Each grp As Object In Groups
GroupName = LDAPPathToGroupName(grp.ToString())
GroupList.Add(GroupName)
Next
Return GroupList
End Function
'グループ名のLDAPパスからグループ名部分を抜き出すメソッド
Private Function LDAPPathToGroupName(ByVal LDAPPath As String) As String
With LDAPPath
Dim spos As Integer = .IndexOf("="c) + 1
Dim epos As Integer = .IndexOf(","c)
If epos > 0 Then
Return .Substring(spos, epos - spos)
Else
Return .Substring(spos)
End If
End With
End Function
これでグループ名のリストを取得できます。
-
有難うございます。教えて頂き、以下の方法で取得できました。
ADSIについてもこれから、試してみようと思います。感謝です。
using System.Security.Principal;
IdentityReferenceCollection irc = new IdentityReferenceCollection();
irc = WindowsIdentity.GetCurrent().Groups;
foreach (IdentityReference ir in irc)
{
IdentityReference grp = ir.Translate(typeof(NTAccount));
TextBox1.Text += "【" + grp + "】" + "\r\n";
}