IT学习联盟

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
IT学习联盟 门户 文章 VB 查看内容

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

2012-8-18 17:27| 发布者: admin| 查看: 545| 评论: 0

[vb]
Imports System.DirectoryServices 
Imports System.Security.Principal 

[vb]
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 


Archiver|IT学习联盟| 网站地图

Powered by Discuz! X2.5© 2001-2010 世界学习室 大伟制作

GMT+8, 2018-10-15 20:29 , Processed in 0.069793 second(s), 12 queries .