VB.Net中使用LDAP验证基于微软AD的登录用户

前端之家收集整理的这篇文章主要介绍了VB.Net中使用LDAP验证基于微软AD的登录用户前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Imports System.DirectoryServices
Imports System.Security.Principal

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As Boolean = GetUserSID("MyADServer","username","password").Length > 0
        MsgBox(r)
    End Sub

    Public Shared Function GetUserSID(ByVal ldapServer As String,ByVal userName As String,ByVal password As String) As String
        Dim strPath As String
        If ldapServer.IndexOf("."c) <> -1 Then
            strPath = String.Format("LDAP://{0}",ldapServer)
        Else
            strPath = String.Format("WinNT://{0}/{1},user",ldapServer,userName)
        End If
        Dim entry As New DirectoryEntry(strPath,userName,password)
        Try
            Return New SecurityIdentifier(entry.Properties("objectSid").Value,0).Value
        Catch
            Return ""
        Finally
            entry.Dispose()
        End Try
    End Function

猜你在找的VB相关文章