Ver Mensaje Individual
  #2 (permalink)  
Antiguo 21/07/2010, 13:16
Avatar de jaullo
jaullo
 
Fecha de Ingreso: abril-2009
Mensajes: 994
Antigüedad: 15 años
Puntos: 30
Respuesta: Cambio de Clave en el Ldap

Da una mirada a esto, puede que te oriente

Código ASP:
Ver original
  1. Private Sub ChangeUserADPassword(ByVal Username As String, ByVal Password As String, ByVal newPwd As String)
  2.  
  3.                 Dim dcDNS As String = "whatever.com"
  4.                 Dim rootDN As String
  5.                 Dim rootDSE As DirectoryEntry
  6.                 Dim searchRoot As DirectoryEntry
  7.                 Dim userEntry As DirectoryEntry
  8.                 Dim searcher As DirectorySearcher
  9.                 Dim results As SearchResultCollection
  10.                 Dim result As SearchResult
  11.                 Dim oldPassword As String = Password
  12.                 Dim newPassword As String = newPwd
  13.  
  14.                 Try
  15.                         '=========================================================================
  16.                         'Here I am binding the directory to the root with the current
  17.                         'users name and password instead of using an admin login to authenticate
  18.                         'The reason for this is that the users are not admin and only admin
  19.                         'can use the setpassword invoke method. thus, authenticated users will
  20.                         'use the change password method
  21.                         'note the authenicationtypes here
  22.                         'you need to either use SecureSocketsLayer or Kerberos (Secure + Sealing)
  23.  
  24.                         result = Nothing
  25.  
  26.                         rootDSE = New DirectoryEntry(String.Format("LDAP://{0}/rootDSE", _
  27.                         dcDNS), Username, oldPassword, AuthenticationTypes.Secure Or _
  28.                         AuthenticationTypes.Sealing Or AuthenticationTypes.ServerBind)
  29.  
  30.                         rootDN = DirectCast(rootDSE.Properties("defaultNamingContext").Value, String)
  31.                         searchRoot = New DirectoryEntry(String.Format("LDAP://{0}/{1}", _
  32.                         dcDNS, rootDN), Username, oldPassword, AuthenticationTypes.Secure Or _
  33.                         AuthenticationTypes.Sealing Or AuthenticationTypes.ServerBind)
  34.                         '==================================================================
  35.                         '------------------------------------------------------------------------
  36.                         'Find the user by their username in the directory using the
  37.                         'DirectorySearcher()
  38.  
  39.                         searcher = New DirectorySearcher(searchRoot)
  40.                         searcher.Filter = String.Format("sAMAccountName={0}", Username)
  41.                         searcher.SearchScope = SearchScope.Subtree
  42.                         searcher.CacheResults = False
  43.  
  44.                         results = searcher.FindAll
  45.  
  46.                         '-------------------------------------------------------------------------
  47.  
  48.                         '*****************************************************
  49.                         For Each result In results
  50.                                 'only use this method on .NET 1.1 or higher
  51.                                 'otherwise, get the adsPath value and build a new
  52.                                 'DirectoryEntry with the supplied credentials
  53.                                 userEntry = result.GetDirectoryEntry()
  54.                                 Exit For
  55.                                 'this is redundant because sAMAccountName is unique
  56.                                 'in the domain, but it is done for clarity
  57.                                 'Bind the user's DirectoryEntry (found from result search)
  58.                         Next
  59.                         'result = Nothing
  60.                         userEntry = result.GetDirectoryEntry()
  61.                         If userEntry Is Nothing Then
  62.                                 Label4.Text = "User not found in this domain."
  63.                                 Exit Sub
  64.                         End If
  65.  
  66.                         'Invoke the ChangePassword method (not the SetPassword method, since that
  67.                         'is used by admins to reset a password)
  68.  
  69.                         userEntry.Invoke("ChangePassword", New Object() {oldPassword, newPassword})
  70.                         userEntry.CommitChanges()
  71.  
  72.                         '****************************************************
  73.  
  74.                         Label4.Text = "Password Changed Successfully"
  75.                         If Not Session("User") Is Nothing Then
  76.                                 txtuser.Text = CStr(Session("User"))
  77.                                 GetUserPasswordADInfo(CStr(Session("User")))
  78.                         Else
  79.                                 GetUserPasswordADInfo(Trim(txtuser.Text))
  80.                         End If
  81.                 Catch ex As Exception 'System.Reflection.TargetInvocationException
  82.                         Label4.Text = ex.Message
  83.                 Finally 'these prevent other memory leaks
  84.                         userEntry = Nothing
  85.                         If Not userEntry Is Nothing Then userEntry.Dispose()
  86.                         results = Nothing
  87.                         If Not results Is Nothing Then results.Dispose()
  88.                         searcher = Nothing
  89.                         If Not searcher Is Nothing Then searcher.Dispose()
  90.                         searchRoot = Nothing
  91.                         If Not searchRoot Is Nothing Then searchRoot.Dispose()
  92.                         rootDSE = Nothing
  93.                         If Not rootDSE Is Nothing Then rootDSE.Dispose()
  94.                 End Try
  95.  
  96.         End Sub
__________________
http://geekswithblogs.net/jaullo/Default.aspx
Si te he ayudado regalame Karma positivo!!!