Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, June 26, 2014 9:32 AM
Hi Dear All,
I have been trying to change and reset password of user in LDAP.
I tried several ways to accomplish the task but could not succeed , please Guid me the correct way to accomplish this
I need urgent Help.
Thanks!
All replies (3)
Thursday, July 3, 2014 5:58 AM ✅Answered | 3 votes
Hi,
You can try some codes below:
public static string ChangePassword20(string adminUser, string adminPassword,
string container, string domainController, string userName, string newPassword)
{
const AuthenticationTypes authenticationTypes = AuthenticationTypes.Secure |
AuthenticationTypes.Sealing | AuthenticationTypes.ServerBind;
DirectoryEntry searchRoot = null;
DirectorySearcher searcher = null;
DirectoryEntry userEntry = null;
try
{
searchRoot = new DirectoryEntry(String.Format("LDAP://{0}/{1}",
domainController, container),
adminUser, adminPassword, authenticationTypes);
searcher = new DirectorySearcher(searchRoot);
searcher.Filter = String.Format("sAMAccountName={0}", userName);
searcher.SearchScope = SearchScope.Subtree;
searcher.CacheResults = false;
SearchResult searchResult = searcher.FindOne(); ;
if (searchResult == null) return "User Not Found In This Domain";
userEntry = searchResult.GetDirectoryEntry();
userEntry.Invoke("SetPassword", new object[] { newPassword });
userEntry.CommitChanges();
return "New password set";
}
catch (Exception ex)
{
return ex.ToString();
}
finally
{
if (userEntry != null) userEntry.Dispose();
if (searcher != null) searcher.Dispose();
if (searchRoot != null) searchRoot.Dispose();
}
}
Usage:
ChangePassword20(@"DOMAIN\Administrator", "password", "DC=Domain,DC=COM",
"domainControllerName", "userName", "newPassword");
Best Wishes!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.
Thursday, June 26, 2014 10:49 AM
If you can share the code, we can try helping you
Muthukrishnan Ramasamy
net4.rmkrishnan.net
Use only what you need, Reduce global warming
Thursday, June 26, 2014 11:08 AM
Hello ,
i am getting the error at FindOne() method. is there i am missing , please help me.
string AdminAccountName = "admin";
string AdminPassword = "pwd";
string Username = "user1";
string NewPassword = "password";
string sPath = "LDAP://path"; //This is if your domain was my.domain.com
DirectoryEntry de = new DirectoryEntry(sPath, "" + AdminAccountName + "", "" + AdminPassword + "", AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(de);
string qry = string.Format("(&(objectCategory=person)(sAMAccountName={0}))", Username);
ds.Filter = qry;
ds.Sort.PropertyName = "CN";
try
{
SearchResult sr = ds.FindOne();
DirectoryEntry user = sr.GetDirectoryEntry();
user.Invoke("SetPassword", new object[]{"" + NewPassword + ""});
user.CommitChanges();
}
finally
{
}