protected void Button1_Click(object sender, EventArgs e)
{
if (this.txtNewPassword.Text.Length >= 0)
{
if (this.txtNewPassword.Text.Equals(this.txtConfirmPassword.Text))
{
DirectoryEntry entry;
char[] sep ={ '\\' };
string[] strArray = this.Context.User.Identity.Name.Split(sep);
if (this.isLocalAccount())
{
try
{
// Connect to Active Directory and get the DirectoryEntry object.
// Note, ADPath is an Active Directory path pointing to a user.
entry = new DirectoryEntry("WinNT://" + strArray[0] + "/" + strArray[1], this.Context.User.Identity.Name, this.txtOldPassword.Text, AuthenticationTypes.Secure);
}
catch (Exception exception1)
{
lblMsg.Text = exception1.Message.ToString();
return;
}
try
{
object objectValue = RuntimeHelpers.GetObjectValue(entry.Invoke("ChangePassword", new object[] { this.txtOldPassword.Text, this.txtNewPassword.Text }));
}
catch (Exception exception5)
{
lblMsg.Text = exception5.Message.ToString();
return;
}
}
else
{
try
{
string[] propertiesToLoad = new string[] { "sAMAccountName", "cn" };
DirectorySearcher searcher = new DirectorySearcher("(sAMAccountName=" + strArray[1] + ")", propertiesToLoad);
searcher.SearchRoot.Username = this.Context.User.Identity.Name;
searcher.SearchRoot.Password = this.txtOldPassword.Text;
// You would have created this which searches AD for the specified user
// and returns its DirectoryEntry object or path. See here.
SearchResult result = searcher.FindOne();
if (result == null)
{
lblMsg.Text = "The User was not found in the Active Directory";
}
entry = new DirectoryEntry(result.Path, this.Context.User.Identity.Name, this.txtOldPassword.Text);
// entry.RefreshCache();
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "sAMAccountName=" + strArray[1].ToString(); ;
SearchResult result1 = search.FindOne();
DirectoryEntry user = result1.GetDirectoryEntry();
string NewPassword = this.txtNewPassword.Text;
user.Invoke("SetPassword", new object[] { NewPassword });
lblMsg.Text = "Password has been changed successfully.";
}
catch (Exception exception6)
{
lblMsg.Text = exception6.Message.ToString();
return;
}
}
// lblMsg.Text = lblMsg.Text + this.SuccessMessage;
}
else
{
// lblMsg.Text = lblMsg.Text + this.ErrorMessagePasswordMatch;
}
}
else
{
lblMsg.Text = "Password doesn’t match requirements.";
}
}