Active Directory - Change Password
Imports System.DirectoryServices
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub ChangeUserADPassword(ByVal Username As String, ByVal Password As String, ByVal newPwd As String)
Dim dcDNS As String = "rockworth.local"
Dim rootDN As String
Dim rootDSE As DirectoryEntry
Dim searchRoot As DirectoryEntry
Dim userEntry As DirectoryEntry
Dim searcher As DirectorySearcher
Dim results As SearchResultCollection
Dim result As SearchResult
Dim oldPassword As String = Password
Dim newPassword As String = newPwd
Try
result = Nothing
rootDSE = New DirectoryEntry(String.Format("LDAP://{0}/rootDSE",dcDNS), Username, oldPassword, AuthenticationTypes.Secure Or AuthenticationTypes.Sealing Or AuthenticationTypes.ServerBind)
rootDN = DirectCast(rootDSE.Properties("defaultNamingContext").Value, String)
searchRoot = New DirectoryEntry(String.Format("LDAP://{0}/{1}", dcDNS, rootDN), Username, oldPassword, AuthenticationTypes.Secure Or AuthenticationTypes.Sealing Or AuthenticationTypes.ServerBind)
'==================================================================
'------------------------------------------------------------------------
'Find the user by their username in the directory using the
'DirectorySearcher()
searcher = New DirectorySearcher(searchRoot)
searcher.Filter = String.Format("sAMAccountName={0}", Username)
searcher.SearchScope = SearchScope.Subtree
searcher.CacheResults = False
results = searcher.FindAll
'-------------------------------------------------------------------------
'*****************************************************
For Each result In results
userEntry = result.GetDirectoryEntry()
Exit For
Next
userEntry = result.GetDirectoryEntry()
If userEntry Is Nothing Then
Label1.Text = "User not found in this domain."
Exit Sub
End If
'Invoke the ChangePassword method (not the SetPassword method, since that
'is used by admins to reset a password)
userEntry.Invoke("ChangePassword", New Object() {oldPassword, newPassword})
userEntry.CommitChanges()
'****************************************************
Label1.Text = "Password Changed Successfully"
Catch ex As Exception 'System.Reflection.TargetInvocationException
Label1.Text = ex.Message
Finally 'these prevent other memory leaks
userEntry = Nothing
If Not userEntry Is Nothing Then userEntry.Dispose()
results = Nothing
If Not results Is Nothing Then results.Dispose()
searcher = Nothing
If Not searcher Is Nothing Then searcher.Dispose()
searchRoot = Nothing
If Not searchRoot Is Nothing Then searchRoot.Dispose()
rootDSE = Nothing
If Not rootDSE Is Nothing Then rootDSE.Dispose()
End Try
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ChangeUserADPassword("5403907", "1234", "1111")
End Sub
End Class
Partial Class _Default
Inherits System.Web.UI.Page
Private Sub ChangeUserADPassword(ByVal Username As String, ByVal Password As String, ByVal newPwd As String)
Dim dcDNS As String = "rockworth.local"
Dim rootDN As String
Dim rootDSE As DirectoryEntry
Dim searchRoot As DirectoryEntry
Dim userEntry As DirectoryEntry
Dim searcher As DirectorySearcher
Dim results As SearchResultCollection
Dim result As SearchResult
Dim oldPassword As String = Password
Dim newPassword As String = newPwd
Try
result = Nothing
rootDSE = New DirectoryEntry(String.Format("LDAP://{0}/rootDSE",dcDNS), Username, oldPassword, AuthenticationTypes.Secure Or AuthenticationTypes.Sealing Or AuthenticationTypes.ServerBind)
rootDN = DirectCast(rootDSE.Properties("defaultNamingContext").Value, String)
searchRoot = New DirectoryEntry(String.Format("LDAP://{0}/{1}", dcDNS, rootDN), Username, oldPassword, AuthenticationTypes.Secure Or AuthenticationTypes.Sealing Or AuthenticationTypes.ServerBind)
'==================================================================
'------------------------------------------------------------------------
'Find the user by their username in the directory using the
'DirectorySearcher()
searcher = New DirectorySearcher(searchRoot)
searcher.Filter = String.Format("sAMAccountName={0}", Username)
searcher.SearchScope = SearchScope.Subtree
searcher.CacheResults = False
results = searcher.FindAll
'-------------------------------------------------------------------------
'*****************************************************
For Each result In results
userEntry = result.GetDirectoryEntry()
Exit For
Next
userEntry = result.GetDirectoryEntry()
If userEntry Is Nothing Then
Label1.Text = "User not found in this domain."
Exit Sub
End If
'Invoke the ChangePassword method (not the SetPassword method, since that
'is used by admins to reset a password)
userEntry.Invoke("ChangePassword", New Object() {oldPassword, newPassword})
userEntry.CommitChanges()
'****************************************************
Label1.Text = "Password Changed Successfully"
Catch ex As Exception 'System.Reflection.TargetInvocationException
Label1.Text = ex.Message
Finally 'these prevent other memory leaks
userEntry = Nothing
If Not userEntry Is Nothing Then userEntry.Dispose()
results = Nothing
If Not results Is Nothing Then results.Dispose()
searcher = Nothing
If Not searcher Is Nothing Then searcher.Dispose()
searchRoot = Nothing
If Not searchRoot Is Nothing Then searchRoot.Dispose()
rootDSE = Nothing
If Not rootDSE Is Nothing Then rootDSE.Dispose()
End Try
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ChangeUserADPassword("5403907", "1234", "1111")
End Sub
End Class
Comments
Post a Comment