rss
twitter
facebook

Home

How To Change a User Password with C# and Active Directory

using System;
using System.DirectoryServices;

class Testclass
{
       
static void Main()
       
{
               
string userName = "Bob";
               
string oldPassword = "123shoot"
               
string newPassword = "KJ#$#H";

               
Console.WriteLine("changing password for " + userName + " from "  
                                       
+ oldPassword + " to " + newPassword);

               
ChangePassword(userName, oldPassword, newPassword);

       
}

       
public static void ChangePassword(string userName, string oldPassword, string newPassword)
       
{
               
string path = "LDAP://CN=" + userName + ",CN=Users,DC=demo,DC=domain,DC=com";

               
//Instantiate a new DirectoryEntry using an administrator uid/pwd
               
//In real life, you'd store the admin uid/pwd  elsewhere
               
DirectoryEntry directoryEntry = new DirectoryEntry(path, "administrator", "password");

               
try
               
{
                   directoryEntry
.Invoke("ChangePassword", new object[]{oldPassword, newPassword});
               
}
               
catch (Exception ex)  //TODO: catch a specific exception ! :)
               
{
                   
Console.WriteLine(ex.Message);
               
}

               
Console.WriteLine("success");
       
}
}


http://www.rootsilver.com/2007/08/how-to-change-a-user-password.html

2 comentários:

Anónimo disse...

Very nice, super cleaner and little code, but work.
Regards.
Jether Rodrigues

Unknown disse...

Why do you need to specify administrator user name and password?

 
Powered by Blogger