Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to update user password in Active directory

Status
Not open for further replies.

bnymk

Programmer
Feb 7, 2003
296
US
Hi all;

I have created a webform that has three text fields, txtOldPassword, txtNewPassword, txtVerifyPassword. What I was trying to do is based on user's logon information, I will display his/her username on the top of the page and allow them to change their password. But unfortunately I'm not being successful in doing that and was hoping to have somebody point me in the right direction. The code that I have below is not doing anything that I expected to do. Could someone please help????

Thanks.



using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;

namespace UpdateADUser
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label titleLbl;
protected System.Web.UI.WebControls.Label userNameLbl;
protected System.Web.UI.WebControls.Label userNameDisplayLbl;
protected System.Web.UI.WebControls.Label passwordLbl;
protected System.Web.UI.WebControls.Label verifyPasswordLbl;
protected System.Web.UI.WebControls.Label messageLbl;
protected System.Web.UI.WebControls.TextBox txtVerifyPassword;
protected System.Web.UI.WebControls.Button submitBtn;
protected System.Web.UI.WebControls.Label oldPasswordLbl;
protected System.Web.UI.WebControls.TextBox txtOldPassword;
protected System.Web.UI.WebControls.TextBox txtNewPassword;

private string userLogonName = HttpContext.Current.User.Identity.Name.ToString();

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
userNameDisplayLbl.Text = userLogonName.Replace(@"DOMAIN\", "");
}

public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = new DirectoryEntry ("LDAP://domain.com/OU=Users,OU=Marketing,OU=AllUsers,DC=domain,DC=com", "Administrator", "Password", AuthenticationTypes.Secure);

return de;
}

public bool UserExists(string UserName)
{
DirectoryEntry de = new DirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();

deSearch.SearchRoot = de;
deSearch.Filter = "(sAMAccountName=" + UserName + ")";

SearchResultCollection results = deSearch.FindAll();

if(results.Count == 0)
{
messageLbl.Text = "UserExists false: " + results.Count.ToString();
return false;
}
else
{
messageLbl.Text = "UserExists true: " + results.Count.ToString();
return true;
}
}

private string FindName(string userAccount)
{
DirectoryEntry entry = GetDirectoryEntry();
string account = userAccount.Replace(@"DOMAIN\", "");

try
{
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(sAMAccountName=" + account + ")";
SearchResult result = search.FindOne();

search.PropertiesToLoad.Add("displayName");



if (result != null)
{
return result.Properties["displayName"][0].ToString();
}
else
{
return "Unknown User";
}
}
catch (Exception ex)
{
string debug = ex.Message;
return "";
}
}
public void ModifyUser(string userDisplayName, String User, string Password)
{
DirectoryEntry de = GetDirectoryEntry();
de.Username = User;
de.Password = Password;
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = ("(&(objectclass=user)(objectcategory=person)(displayname=" + userDisplayName + "))");
ds.SearchScope = SearchScope.Subtree;
SearchResult results = ds.FindOne();

if(results != null)
{
try
{
DirectoryEntry updateEntry = results.GetDirectoryEntry();
updateEntry.Invoke("setpassword", Password);
updateEntry.CommitChanges();
updateEntry.Close();
}
catch (Exception ex)
{
string error = ex.Message.ToString();
}
}
de.Close();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.submitBtn.Click += new System.EventHandler(this.submitBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void submitBtn_Click(object sender, System.EventArgs e)
{
if(txtNewPassword.Text.ToString() != "" && txtOldPassword.Text.ToString() != "" && txtVerifyPassword.Text.ToString() != "")
{
string User = userLogonName.Replace(@"DOMAIN\", "").ToString();
string oldPassword = txtOldPassword.Text.ToString();
string Password = txtNewPassword.Text.ToString();

if(UserExists(FindName(userLogonName).ToString()) == true)
{
ModifyUser(FindName(userLogonName).ToString(), User, Password);
}
}
}
}
}


"Behind every great fortune there lies a great crime", Honore De Balzac
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top