2010-11-23 15 views
5

cómo cambiar la contraseña de la cuenta de usuario, por código C#?cómo cambiar la contraseña de la cuenta de usuario, por código C#?

+0

Qué usuario de ¿cuenta? ¿Dominio? Cualquier aplicación? ¿Sistema? –

+0

¿Qué cuenta de usuario ?????? – TalentTuner

+0

Podría ser una tontería de esta pregunta (dependiendo de qué contraseña quiera cambiar): http://stackoverflow.com/questions/234845/change-local-administrator-password-in-c –

Respuesta

5

Uso del directorio activo:

// Connect to Active Directory and get the DirectoryEntry object. 
// Note, ADPath is an Active Directory path pointing to a user. You would have created this 
// path by calling a GetUser() function, which searches AD for the specified user 
// and returns its DirectoryEntry object or path. See http://www.primaryobjects.com/CMS/Article61.aspx 
DirectoryEntry oDE; 
oDE = new DirectoryEntry(ADPath, ADUser, ADPassword, AuthenticationTypes.Secure); 

try 
{ 
    // Change the password. 
    oDE.Invoke("ChangePassword", new object[]{strOldPassword, strNewPassword}); 
} 
catch (Exception excep) 
{ 
    Debug.WriteLine("Error changing password. Reason: " + excep.Message); 
} 

Aquí tienes ejemplo para cambiarla en la cuenta de usuario local:

http://msdn.microsoft.com/en-us/library/ms817839

Otra alternativa podría ser el uso de la interoperabilidad y llamar al código no administrado: netapi32.dll

http://msdn.microsoft.com/en-us/library/aa370650(VS.85).aspx

+0

Gracias por su ayuda, ¿cómo puedo obtener ADPath? necesidad de usar con ActiveDS, de ser así, ¿cómo? –

+0

Lo sentimos, el enlace en el código está oculto, quizás el desbordamiento de la pila podría mejorar esto :-) Aquí está el enlace al uso general de AD y cómo construir el ADPath: http://www.primaryobjects.com/CMS/Article61 .aspx –

1
 DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); 
     DirectoryEntry grp; 
     grp = AD.Children.Find("test", "user"); 
     if (grp != null) 
     { 
      grp.Invoke("SetPassword", new object[] { "test" }); 
     } 
     grp.CommitChanges(); 
     MessageBox.Show("Account Change password Successfully"); 

"correr en administrador para cambiar todo usuario

4

Aquí está una manera más sencilla de hacer esto, sin embargo se necesita hacer referencia System.DirectoryServices.AccountManagement de .Net 4,0

namespace PasswordChanger 
{ 
    using System; 
    using System.DirectoryServices.AccountManagement; 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      ChangePassword("domain", "user", "oldpassword", "newpassword"); 
     } 

     public static void ChangePassword(string domain, string userName, string oldPassword, string newPassword) 
     { 
      try 
      { 
       using (var context = new PrincipalContext(ContextType.Domain, domain)) 
       using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName)) 
       { 
        user.ChangePassword(oldPassword, newPassword); 
       } 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex); 
      } 
     } 
    } 
} 
+0

Funciona muy bien incluso sin privilegios de administrador, gracias Paul. – mberna

Cuestiones relacionadas