WSS 2.0 : How to reset Password in WSS - Account Creation Mode
Consider the following scenario. You installed WSS in Account Creation Mode. If a user forgets their password, they currently have to contact the admin and
they in turn need to go into the site administration and manually reset the password. This issue has become more prevalent and will detract the Admin from normal duties.
So I would like to have a way to automate password rests for the users. Users will be provided a webpart of other link that will reset the password and send out an email with the new password to the users account email address.
Here you go for a sample code.
public class MyChangePassword : Microsoft.SharePoint.WebPartPages.WebPart
{
//Button for Change Password
Button btnChangePassword;
string strError = "";
protected override void CreateChildControls()
{
//Create button
btnChangePassword= new Button();
btnChangePassword.Width = 100;
btnChangePassword.Text = "Change Password";
btnChangePassword.Click += new EventHandler(btnChangePassword_Click);
this.Controls.Add(btnChangePassword);
}
private void btnChangePassword_Click(object sender, EventArgs e)
{
try
{
//Gets the context of the web
SPWeb webContext = SPControl.GetContextWeb(Context);
string strLoginName = webContext.CurrentUser.LoginName;
//Get the impersation context
WindowsImpersonationContext wic = CreateIdentity("username", "domain", "password").Impersonate();
SPUtility.ChangeAccountPassword(webContext, strLoginName, "", "newpassword");
//undo the impersonation context
wic.Undo();
strError = "Password Changed";
}
catch(Exception ee)
{
strError += ee.ToString();
}
}
protected override void RenderWebPart(HtmlTextWriter output)
{
btnChangePassword.RenderControl(output);
output.Write(strError);
}
protected static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
{
// The Windows NT user token.
IntPtr tokenHandle = new IntPtr(0);
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3;
tokenHandle = IntPtr.Zero;
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(User, Domain, Password,
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
throw new Exception("LogonUser failed with error code: " + ret);
}
System.Diagnostics.Debug.WriteLine("Created user token: " + tokenHandle);
//The WindowsIdentity class makes a new copy of the token.
//It also handles calling CloseHandle for the copy.
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
[DllImport("advapi32.dll", SetLastError=true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
}
Comments
- Anonymous
January 22, 2009
PingBack from http://www.hilpers.fr/943926-wss3-creation-dune-webpart