Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

ADAM - Change Password

  Asked By: Domonique    Date: Jun 15    Category: Sharepoint    Views: 1074

I am trying to create/update an existing code which I used for SQL
FBA Login. Now I am using same code with ADAM FBA Login. This code I
am using for allowing user to change their Password. I tested same
code succesfull with SQL FBA, but with ADAM it is giving error and
throwing exception. Here is the code, please advise whats wrong I am
doing

*------ Code Start
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using ActiveDs;
using System.DirectoryServices;
using System.Web.Security;

namespace ADAMPasswordChange
{
[Guid("ad337165-1119-4ac9-8d0b-2ad102c95245")]
public class ADAMPassword :
Microsoft.SharePoint.WebPartPages.WebPart
{
public ADAMPassword()
{
this.ExportMode = WebPartExportMode.All;
}

private TextBox oldpassword;
private TextBox newpassword;
private TextBox checknewpassword;

private string strLoginName;
private string strUserName;


private LinkButton btn;
private Label output;

protected override void CreateChildControls()
{
this.oldpassword = new TextBox();
this.oldpassword.TextMode = TextBoxMode.Password;
this.Controls.Add(oldpassword);

this.newpassword = new TextBox();
this.newpassword.TextMode = TextBoxMode.Password;
this.Controls.Add(newpassword);

this.checknewpassword = new TextBox();
this.checknewpassword.TextMode = TextBoxMode.Password;
this.Controls.Add(checknewpassword);

this.btn = new LinkButton();
this.btn.Click += new EventHandler(btn_Click);
this.btn.Text = "Change Password Now";
this.Controls.Add(btn);

this.output = new Label();
this.Controls.Add(output);

base.CreateChildControls();
}

void btn_Click(object sender, EventArgs e)
{

if (newpassword.Text.ToString() !=
checknewpassword.Text.ToString())
{
output.Text += "<BR>Passwords don't match....";
return;
}
//if (newpassword.Text.ToString() ==
checknewpassword.Text.ToString())
else
{
Boolean NTUser = true;
try
{
//SPWeb webContext = SPControl.GetContextWeb
(Context);
//strLoginName =
webContext.CurrentUser.LoginName.ToUpper();

GetUserName();
//output.Text += strLoginName;

if (strLoginName != @"SHAREPOINT\SYSTEM")
{

if (!strLoginName.Contains(@"SERVER\"))
{
//forms auth user

NTUser = false;
}
}

}
catch
{
NTUser = false;
}

if (NTUser)
{


}
else
{
try
{
//change password for the forms auth user

//output.Text += strLoginName;
//MembershipUser mUser = Membership.GetUser
(GetUserName());
DirectoryEntry entry = new DirectoryEntry
("LDAP://localhost:389/CN=Users,O=ADAM,C=US", strLoginName,
oldpassword.Text.ToString(), AuthenticationTypes.None);
//mUser.ChangePassword
(oldpassword.Text.ToString(), newpassword.Text.ToString());
try
{
entry.Invoke("SetPassword", new object[]
{ newpassword.Text.ToString() });
entry.CommitChanges();
output.Text += "<BR> Password is
changed
";
}
catch (Exception ex)
{
output.Text += " Password couldn't be
changed due to restrictions 1";
output.Text += ex.Message;


}

output.Text += "<BR> Password is changed
1
";

}
catch (Exception error)
{
output.Text += "1 User Password Change
Error:\n" + error.StackTrace.ToString() + "\n" +
error.Message.ToString();
//output.Text += "<BR>" +
error.Message.ToString();
}
}
}
//else
//{
// output.Text += "<BR>Passwords don't
match....
";
//}
}


protected override void Render(System.Web.UI.HtmlTextWriter
writer)
{
//try
//{
// SPWeb webContext = SPControl.GetContextWeb(Context);
// strLoginName = webContext.CurrentUser.Name.ToUpper
();
//}
//catch (Exception)
//{
// output.Text += "<BR> Please sign in first using
the 'Sign In' button above
";
//}
EnsureChildControls();
GetUserName();
if (strLoginName != string.Empty)
{
writer.Write("<table border=0>");
writer.Write("<tr>");
writer.Write("<td class='ms-vb'>");
writer.Write("User Name:");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write(strLoginName);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr><td colspan='3'></td></tr>");
writer.Write("<tr>");
writer.Write("<td class='ms-vb'>");
writer.Write("Current password:");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
oldpassword.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr valign='top'>");
writer.Write("<td class='ms-vb'>");
writer.Write("New password:");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
newpassword.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("<tr valign='top'>");
writer.Write("<td class='ms-vb'>");
writer.Write("Confirm new password:");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
checknewpassword.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</tr>");
writer.Write("<tr valign='top'>");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
btn.RenderControl(writer);
writer.Write("</td>");
writer.Write("<td class='ms-vb'>");
writer.Write("</td>");
writer.Write("</tr>");
writer.Write("</table>");
output.RenderControl(writer);
}
else
{
output.RenderControl(writer);
}
}

string GetUserName()
{
strUserName = "";

try
{
SPWeb webContext = SPControl.GetContextWeb(Context);

strLoginName = webContext.CurrentUser.Name.ToUpper();
strUserName = webContext.CurrentUser.LoginName.ToUpper
();

string[] x = strUserName.Split(':');
if (x.Length > 1)
{
strUserName = x[1];
}
else
{
strUserName = x[0];
}
}
catch
{
strUserName = "";
}
return strUserName;
}
}
}

*------ Code Ends

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Destiny Lewis     Answered On: Jun 15

Any Expert advise  on following code?

 
Didn't find what you were looking for? Find more on ADAM - Change Password Or get search suggestion and latest updates.




Tagged: