-
AD活动目录操作类(VB.net)
在园子里找到就翻成VB.net的了。
原文见:
http://www.cnblogs.com/rickie/category/29428.html
对这个类的使用,我会在下篇post中补上
开始要添加对System.DirectoryServices,System.DirectoryServices.Protocols 这两个DLL的引用。
在公共程序集里面都可以找到。
原文见:
http://www.cnblogs.com/rickie/category/29428.html
对这个类的使用,我会在下篇post中补上
开始要添加对System.DirectoryServices,System.DirectoryServices.Protocols 这两个DLL的引用。
在公共程序集里面都可以找到。
Imports System.DirectoryServices
Imports System.Data
Imports System
Imports Microsoft.VisualBasic
Public Class ADHelper
Public Shared ADPath As String = System.Configuration.ConfigurationManager.AppSettings.Get("ADPath")
Public Shared UserName As String
Public Shared PassWord As String
Public Enum ADAccountOptions
UF_TEMP_DUPLICATE_ACCOUNT = &H100
UF_NORMAL_ACCOUNT = &H200
UF_INTERDOMAIN_TRUST_ACCOUNT = &H800
UF_WORKSTATION_TRUST_ACCOUNT = &H1000
UF_SERVER_TRUST_ACCOUNT = &H2000
UF_DONT_EXPIRE_PASSWD = &H10000
UF_SCRIPT = &H1
UF_ACCOUNTDISABLE = &H2
UF_HOMEDIR_REQUIRED = &H8
UF_LOCKOUT = &H10
UF_PASSWD_NOTREQD = &H20
UF_PASSWD_CANT_CHANGE = &H40
UF_ACCOUNT_LOCKOUT = &H10
UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = &H80
End Enum
Public Enum LoginResult
LOGIN_OK = 0
LOGIN_USER_DOESNT_EXIST
LOGIN_USER_ACCOUNT_INACTIVE
End Enum
Public Shared Function IsUserValid(ByVal UserName As String, ByVal PassWord As String) As Boolean
Dim deUser As DirectoryEntry
deUser = New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
Try
Dim native As Object = deUser.NativeObject
Return True
Catch ex As Exception
Return False
Finally
deUser.Close()
End Try
End Function
Public Shared Function IsAccountActive(ByVal userAccountControl As Integer) As Boolean
Dim userAccountControl_Disabled As Integer = Convert.ToInt32(ADAccountOptions.UF_ACCOUNTDISABLE)
Dim flagExists As Integer = userAccountControl And userAccountControl_Disabled
If (flagExists > 0) Then
Return False
Else
Return True
End If
End Function
Public Shared Function Login(ByVal UserName As String, ByVal PassWord As String) As LoginResult
If (IsUserValid(UserName, PassWord)) Then
Dim de As DirectoryEntry = GetUser(UserName)
If (de IsNot DBNull.Value) Then
Dim userAccountControl As Integer = Convert.ToInt32(de.Properties("userAccountControl")(0))
de.Close()
If (Not IsAccountActive(userAccountControl)) Then
Return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE
Else
Return LoginResult.LOGIN_OK
End If
Else
Return LoginResult.LOGIN_USER_DOESNT_EXIST
End If
Else
Return LoginResult.LOGIN_USER_DOESNT_EXIST
End If
End Function
Public Shared Function GetUser(ByVal UserName As String) As DirectoryEntry
Dim de As DirectoryEntry = GetDirectoryObject()
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
de = New DirectoryEntry(results.Path, UserName, PassWord, AuthenticationTypes.Secure)
Return de
Else
Return Nothing
End If
End Function
Public Shared Function GetDirectoryObject() As DirectoryEntry
Dim oDe As DirectoryEntry
oDe = New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
Return oDe
End Function
Public Shared Function GetProperty(ByVal searchResult As SearchResult, ByVal PropertyName As String) As String
If (searchResult.Properties.Contains(PropertyName)) Then
Return searchResult.Properties(PropertyName)(0).ToString
Else
Return String.Empty
End If
End Function
Public Shared Function test(ByVal UserName As String) As SearchResult
Dim de As DirectoryEntry = GetDirectoryObject()
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
Return results
Else
Return Nothing
End If
End Function
Public Shared Function nopassword(ByVal UserName As String) As SearchResult
Dim de As DirectoryEntry = New DirectoryEntry(ADPath)
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
Return results
Else
Return Nothing
End If
End Function
End Class
Imports System.Data
Imports System
Imports Microsoft.VisualBasic
Public Class ADHelper
Public Shared ADPath As String = System.Configuration.ConfigurationManager.AppSettings.Get("ADPath")
Public Shared UserName As String
Public Shared PassWord As String
Public Enum ADAccountOptions
UF_TEMP_DUPLICATE_ACCOUNT = &H100
UF_NORMAL_ACCOUNT = &H200
UF_INTERDOMAIN_TRUST_ACCOUNT = &H800
UF_WORKSTATION_TRUST_ACCOUNT = &H1000
UF_SERVER_TRUST_ACCOUNT = &H2000
UF_DONT_EXPIRE_PASSWD = &H10000
UF_SCRIPT = &H1
UF_ACCOUNTDISABLE = &H2
UF_HOMEDIR_REQUIRED = &H8
UF_LOCKOUT = &H10
UF_PASSWD_NOTREQD = &H20
UF_PASSWD_CANT_CHANGE = &H40
UF_ACCOUNT_LOCKOUT = &H10
UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = &H80
End Enum
Public Enum LoginResult
LOGIN_OK = 0
LOGIN_USER_DOESNT_EXIST
LOGIN_USER_ACCOUNT_INACTIVE
End Enum
Public Shared Function IsUserValid(ByVal UserName As String, ByVal PassWord As String) As Boolean
Dim deUser As DirectoryEntry
deUser = New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
Try
Dim native As Object = deUser.NativeObject
Return True
Catch ex As Exception
Return False
Finally
deUser.Close()
End Try
End Function
Public Shared Function IsAccountActive(ByVal userAccountControl As Integer) As Boolean
Dim userAccountControl_Disabled As Integer = Convert.ToInt32(ADAccountOptions.UF_ACCOUNTDISABLE)
Dim flagExists As Integer = userAccountControl And userAccountControl_Disabled
If (flagExists > 0) Then
Return False
Else
Return True
End If
End Function
Public Shared Function Login(ByVal UserName As String, ByVal PassWord As String) As LoginResult
If (IsUserValid(UserName, PassWord)) Then
Dim de As DirectoryEntry = GetUser(UserName)
If (de IsNot DBNull.Value) Then
Dim userAccountControl As Integer = Convert.ToInt32(de.Properties("userAccountControl")(0))
de.Close()
If (Not IsAccountActive(userAccountControl)) Then
Return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE
Else
Return LoginResult.LOGIN_OK
End If
Else
Return LoginResult.LOGIN_USER_DOESNT_EXIST
End If
Else
Return LoginResult.LOGIN_USER_DOESNT_EXIST
End If
End Function
Public Shared Function GetUser(ByVal UserName As String) As DirectoryEntry
Dim de As DirectoryEntry = GetDirectoryObject()
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
de = New DirectoryEntry(results.Path, UserName, PassWord, AuthenticationTypes.Secure)
Return de
Else
Return Nothing
End If
End Function
Public Shared Function GetDirectoryObject() As DirectoryEntry
Dim oDe As DirectoryEntry
oDe = New DirectoryEntry(ADPath, UserName, PassWord, AuthenticationTypes.Secure)
Return oDe
End Function
Public Shared Function GetProperty(ByVal searchResult As SearchResult, ByVal PropertyName As String) As String
If (searchResult.Properties.Contains(PropertyName)) Then
Return searchResult.Properties(PropertyName)(0).ToString
Else
Return String.Empty
End If
End Function
Public Shared Function test(ByVal UserName As String) As SearchResult
Dim de As DirectoryEntry = GetDirectoryObject()
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
Return results
Else
Return Nothing
End If
End Function
Public Shared Function nopassword(ByVal UserName As String) As SearchResult
Dim de As DirectoryEntry = New DirectoryEntry(ADPath)
Dim deSearch As New DirectorySearcher
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + UserName + "))"
deSearch.SearchScope = SearchScope.Subtree
Dim results As SearchResult = deSearch.FindOne
If (results IsNot DBNull.Value) Then
Return results
Else
Return Nothing
End If
End Function
End Class
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式