●説明:例
・domainServerId:192.168.66.88
・domainServerName:AAA.co.jp
・userid:panxy
→panxy:CNの名=潘 暁宇/
→ADDomain:OU=JP
→Group名:TestGroupName
・PassWord:******
●LdapAuthentication.vb
Imports System.Collections
Imports System.Text
Imports System.DirectoryServices
Public Class LdapAuthentication
''' <summary>
''' ldapPath
''' </summary>
''' <remarks></remarks>
Dim _path As String
''' <summary>
''' ADユーザー名cn=潘 暁宇
''' </summary>
''' <remarks></remarks>
Dim _filterAttribute As String
Public Sub New(ByVal domainServerId As String)
_path = "LDAP://" & domainServerId
End Sub
''' <summary>
''' ADドメインユーザーチェック
''' </summary>
''' <param name="domainServerId"></param>
''' <param name="domainServerName"></param>
''' <param name="userid"></param>
''' <param name="pwd"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function IsAuthenticated(ByVal domainServerId As String, ByVal domainServerName As String, ByVal userid As String, ByVal pwd As String) As Boolean
Dim domainAndUsername As String = domainServerName & "\" & userid
Dim entry As DirectoryEntry = New DirectoryEntry(_path, domainAndUsername, pwd)
Try
Dim search As DirectorySearcher = New DirectorySearcher(entry)
'Bind to the native AdsObject to force authentication.
Dim obj As Object = entry.NativeObject
search.Filter = "(SAMAccountName=" & userid & ")"
search.PropertiesToLoad.Add("cn")
search.PropertiesToLoad.Add("displayName")
Dim result As SearchResult = search.FindOne()
If (result Is Nothing) Then
'ユーザー名情報チェックできない(入力したID、パスワードが間違う)
Return False
Else
'ユーザー名情報チェックOK
_path = result.Path 'LDAP://192.168.66.88/CN=潘 暁宇,OU=JP,DC=AAA,DC=co,DC=jp
_filterAttribute = CType(result.Properties("cn")(0), String)
userName = CType(result.Properties("displayName")(0), String)
userGlobalId = userid
Return True
End If
Catch ex As Exception
Return False
End Try
End Function
''' <summary>
''' ADユーザーのグループ名と権限(admin権限有無)の取得
''' </summary>
''' <param name="userid"></param>
''' <param name="pwd"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function GetUserInfoForGroupNm(ByVal userid As String, ByVal pwd As String) As String
'サーバーのアドレス
'Dim ldapPath As String = "LDAP://" & txtserverIP.Text.Trim 'LDAP://192.168.66.88
Dim entry As DirectoryEntry = New DirectoryEntry(_path, userid, pwd)
Dim search As DirectorySearcher = New DirectorySearcher(entry)
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Add("memberOf")
Dim groupNames As StringBuilder = New StringBuilder()
Try
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer = result.Properties("memberOf").Count
Dim dn As String
Dim equalsIndex, commaIndex
Dim propertyCounter As Integer
For propertyCounter = 0 To propertyCount - 1
dn = CType(result.Properties("memberOf")(propertyCounter), String)
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (equalsIndex = -1) Then
Return Nothing
End If
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1))
groupNames.Append("|")
Next
'情報説明:グループ名(Tokyo)|管理者権限有無(あるの時:Domain Admins/無いの時:空)
'管理者権限持っている情報:TestGroupName|Domain Admins|
'普通ユーザー情報:TestGroupName|
Return groupNames.ToString()
Catch ex As Exception
Throw New Exception("Error obtaining group names. " & ex.Message)
End Try
End Function
Public Function GetUserInfoForGroupNm2(ByVal userid As String, ByVal pwd As String, ByRef arr As ArrayList) As String
'サーバーのアドレス
Dim entry As DirectoryEntry = New DirectoryEntry(_path, userid, pwd)
Dim search As DirectorySearcher = New DirectorySearcher(entry)
search.Filter = "(cn=" & _filterAttribute & ")"
search.PropertiesToLoad.Add("memberOf")
Dim groupNames As StringBuilder = New StringBuilder()
Try
Dim result As SearchResult = search.FindOne()
Dim propertyCount As Integer = result.Properties("memberOf").Count
Dim dn As String
Dim equalsIndex, commaIndex
Dim propertyCounter As Integer
Dim ouArray As New ArrayList()
For propertyCounter = 0 To propertyCount - 1
'CN=admin,OU=Tokyo1,OU=JP1,DC=example,DC=local
'CN=user1,OU=Groups,OU=Tokyo1,OU=JP1,DC=example,DC=local
dn = CType(result.Properties("memberOf")(propertyCounter), String)
Dim ou As String() = dn.Split(","c)
For index = 0 To ou.Count - 1
If ou(index).Contains("OU=") Then
Dim ouNm As String = ou(index).Replace("OU=", String.Empty)
Dim isHaveOu = (From ouItem In ouArray Where ouItem = ouNm
Select ouItem).ToList()
If isHaveOu.Count = 0 Then
ouArray.Add(ou(index).Replace("OU=", String.Empty))
End If
End If
Next
equalsIndex = dn.IndexOf("=", 1)
commaIndex = dn.IndexOf(",", 1)
If (equalsIndex = -1) Then
Return Nothing
End If
groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1))
If propertyCounter <> propertyCount - 1 Then
groupNames.Append(",")
End If
Next
arr = ouArray
'管理者権限持っている情報:Tokyo|Domain Admins|
'普通ユーザー情報:Tokyo|
Return groupNames.ToString()
Catch ex As Exception
Throw New Exception("Error obtaining group names. " & ex.Message)
End Try
End Function
''' <summary>
''' OU名により、グループ名を取得
''' </summary>
''' <returns></returns>
Public Function GetGroupNames() As String
Dim OUTop1 As String = "OU=JP,DC=example,DC=local"
Dim sb As StringBuilder = New StringBuilder()
Dim domainIp As String = "123.456.78.123" 'ADサーバーのIPアドレス
Dim ouName As PrincipalContext = New PrincipalContext(ContextType.Domain, domainIp, OUTop1)
Dim findAllGroups As GroupPrincipal = New GroupPrincipal(ouName)
Dim ps As PrincipalSearcher = New PrincipalSearcher(findAllGroups)
For Each group In ps.FindAll()
'CN(グループ名)情報を絞り込むして、SBに入れるTODO...
sb.Append("|" + group.DistinguishedName)
Next
Return sb.ToString
End Function
''' <summary>
''' グループはADに有無の確認
''' </summary>
''' <param name="groupName"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function IsExistGroup(ByVal groupName As String) As Boolean
Try
'root情報作成
Dim adInfo = _path
Dim dcs As String() = _domainServerNm.Split("."c)
Dim dc As String = String.Empty
For index = 0 To dcs.Count - 1
If Not String.IsNullOrEmpty(dc) Then
dc &= ","
End If
dc &= "DC=" & dcs(index)
Next
Dim root As New DirectoryEntry(_path & "/" & dc, _domainUser, _domainPass)
Dim search As New DirectorySearcher(root)
'グループ & 指定のグループ名 & セキュリティグループ
search.Filter = "(&(objectCategory=Group)(name=" & groupName & ")(groupType:1.2.840.113556.1.4.803:=2147483648))"
Dim result = search.FindOne()
If result Is Nothing Then
Return False
End If
Catch ex As Exception
Return False
End Try
Return True
End Function
//ユーザEmployeeIDの取得
Public Function GetUserEmployeeID(ByVal userid As String, ByVal pwd As String) As String
Dim root As New DirectoryEntry("LDAP://192.168.xxx.xxx/CN=潘 暁宇,OU=JP,DC=example,DC=local", "user1", "******")
Dim obj = root.NativeObject
Dim se As New DirectorySearcher(root)
Dim result As SearchResult = se.FindOne()
Dim de As DirectoryEntry = result.GetDirectoryEntry
If Not String.IsNullOrEmpty(de.Properties("employeeID").Value) Then
Return de.Properties("employeeID").Value
Else
Return String.Empty
End If
End Function
C#:
/// <summary>
/// OU名により、グループ名を取得
/// </summary>
/// <returns></returns>
private string GetGroupNames()
{
string OUTop1 = "OU=親名1,DC=example,DC=local";
string OUTop_childOU = "OU=親名1-子1,OU=親名1,DC=example,DC=local";
string OUTop2 = "OU=親名2,DC=example,DC=local";
string OUTop2_childOU = "OU=親名2-子1,OU=親名2,DC=example,DC=local";
StringBuilder sb = new StringBuilder();
string domainIp = "123.456.78.123"; //ADサーバーのIPアドレス
PrincipalContext ouName = new PrincipalContext(ContextType.Domain, domainIp, OUTop1);
GroupPrincipal findAllGroups = new GroupPrincipal(ouName);
PrincipalSearcher ps = new PrincipalSearcher(findAllGroups);
foreach (var group in ps.FindAll())
{
//CN(グループ名)情報を絞り込むして、SBに入れるTODO...
sb.Append("|" + group.DistinguishedName);
}
return sb.ToString();
}
End Class
●画面
Imports System.Collections
Imports System.Text
Imports System.DirectoryServices
Public Class Form1
Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
txtInfo.Text = String.Empty
Dim ldapauth As LdapAuthentication = New LdapAuthentication(txtserverIP.Text)
Dim isauth As Boolean = ldapauth.IsAuthenticated(txtserverIP.Text.Trim, txtdomainname.Text.Trim, txtloginID.Text, txtloginpass.Text)
If isauth Then
txtInfo.Text = ldapauth.GetUserInfo(txtloginID.Text, txtloginpass.Text)
Else
txtInfo.Text = "ID,PWDをもう一度確認ください!"
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
labLDAP.Text = "LDAP://" & txtserverIP.Text.Trim
End Sub
Private Sub btnGetGroups_Click(sender As Object, e As EventArgs) Handles btnGetGroups.Click
Me.txtInfo.Text = String.Empty
Dim ldapauth As LdapAuthentication = New LdapAuthentication(txtserverIP.Text)
Dim isauth As Boolean = ldapauth.IsAuthenticated(txtserverIP.Text.Trim, txtdomainname.Text.Trim, txtloginID.Text, txtloginpass.Text)
If isauth Then
Dim arry As New ArrayList()
arry.Add("JP")
arry.Add("JP TAX")
txtInfo.Text = ldapauth.GetGroupNames(arry, txtloginID.Text, txtloginpass.Text)
Else
txtInfo.Text = "グループ情報なし!"
End If
End Sub
Private Sub btnGetEmpID_Click(sender As Object, e As EventArgs) Handles btnGetEmpID.Click
Me.txtInfo.Text = String.Empty
Dim ldapauth As LdapAuthentication = New LdapAuthentication(txtserverIP.Text)
Dim isauth As Boolean = ldapauth.IsAuthenticated(txtserverIP.Text.Trim, txtdomainname.Text.Trim, txtloginID.Text, txtloginpass.Text)
If isauth Then
txtInfo.Text = ldapauth.GetUserEmployeeID(txtloginID.Text, txtloginpass.Text)
Else
txtInfo.Text = "EmployeeId情報なし!"
End If
End Sub
End Class
ドメイン操作
最新推荐文章于 2021-07-31 22:13:40 发布