Sunday, January 13, 2008

To change password through webparts

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.ComponentModel;
using System.Collections.Generic;
using System.Web.UI.WebControls;


namespace changepasswordwebpart
{
[Guid("713bc165-73a4-4a65-8651-adf5d5da8a50")]
public class changepasswordwebpart : System.Web.UI.WebControls.WebParts.WebPart,IWebEditable
{
System.Web.UI.WebControls.Label oldlb = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Label userid_lb = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Panel panel = new System.Web.UI.WebControls.Panel();
System.Web.UI.WebControls.TextBox oldpasswordtb = new System.Web.UI.WebControls.TextBox();
System.Web.UI.WebControls.TextBox userid_txt = new System.Web.UI.WebControls.TextBox();
System.Web.UI.WebControls.TextBox newpasswordtb = new System.Web.UI.WebControls.TextBox();
System.Web.UI.WebControls.TextBox Confirmpasswordtb = new System.Web.UI.WebControls.TextBox();
System.Web.UI.WebControls.Button Checkpasswordbttn = new System.Web.UI.WebControls.Button();
System.Web.UI.WebControls.Button cancelbttn = new System.Web.UI.WebControls.Button();
System.Web.UI.WebControls.Label statuslb = new System.Web.UI.WebControls.Label();
System.Web.UI.WebControls.Label Info_lb = new System.Web.UI.WebControls.Label();
public changepasswordwebpart()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
protected override void CreateChildControls()
{
try
{
SPWeb thisWeb = SPControl.GetContextWeb(this.Context);
SPUser currentUser = thisWeb.CurrentUser;
String username = currentUser.Name;
//base.CreateChildControls();
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl("
"));
Info_lb.Text = "All fields are manadatory!";
Info_lb.Width = 300;
Info_lb.Height = 20;
panel.Controls.Add(Info_lb);
panel.Controls.Add(new LiteralControl("
"));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl(""));
panel.Controls.Add(new LiteralControl("
"));
userid_lb.Text = "UserID:";
userid_lb.Width = 150;
userid_lb.Height = 20;
panel.Controls.Add(userid_lb);
panel.Controls.Add(new LiteralControl("
"));
userid_txt.Text = username;
userid_txt.ID = "usernametb";
userid_txt.Width = 150;
userid_txt.Height = 20;
userid_txt.ReadOnly = true;
panel.Controls.Add(userid_txt);
panel.Controls.Add(new LiteralControl("
"));
panel.Controls.Add(new LiteralControl("
"));
oldlb.Text = "Old Password";
oldlb.Width = 150;
oldlb.Height = 20;
panel.Controls.Add(oldlb);
panel.Controls.Add(new LiteralControl("
"));
oldpasswordtb.Text = "";
//oldpasswordtb.ID = "oldtb";
oldpasswordtb.Width = 150;
oldpasswordtb.Height = 20;
oldpasswordtb.TextMode = TextBoxMode.Password;
panel.Controls.Add(oldpasswordtb);
panel.Controls.Add(new LiteralControl("
"));

System.Web.UI.WebControls.Label newlb = new System.Web.UI.WebControls.Label();
newlb.Text = "New Password";
newlb.Width = 150;
newlb.Height = 20;
panel.Controls.Add(newlb);
panel.Controls.Add(new LiteralControl("
"));
newpasswordtb.Text = "";
//newpasswordtb.ID = "newpwdtb";
newpasswordtb.Width = 150;
newpasswordtb.Height = 20;
newpasswordtb.TextMode = TextBoxMode.Password;
panel.Controls.Add(newpasswordtb);
panel.Controls.Add(new LiteralControl("
"));
System.Web.UI.WebControls.Label confirmlb = new System.Web.UI.WebControls.Label();
confirmlb.Text = "Confirm Password";
confirmlb.Width = 150;
confirmlb.Height = 20;
panel.Controls.Add(confirmlb);
panel.Controls.Add(new LiteralControl("
"));
Confirmpasswordtb.Text = "";
//Confirmpasswordtb.ID = "Confirmtb";
Confirmpasswordtb.Width = 150;
Confirmpasswordtb.Height = 20;
Confirmpasswordtb.TextMode = TextBoxMode.Password;
panel.Controls.Add(Confirmpasswordtb);
panel.Controls.Add(new LiteralControl("
"));

panel.Controls.Add(new LiteralControl("
"));
Checkpasswordbttn.Text = "Ok";
Checkpasswordbttn.Width = 100;
//Checkpasswordbttn.ID = "okbttn";
Checkpasswordbttn.Click += new EventHandler(Checkpasswordbttn_Click);
panel.Controls.Add(Checkpasswordbttn);
cancelbttn.Text = "Cancel";
cancelbttn.Visible = false;
Controls.Add(cancelbttn);
panel.Controls.Add(new LiteralControl("
"));

panel.Controls.Add(new LiteralControl("
"));
panel.Controls.Add(new LiteralControl("
"));
statuslb.Text = "";
statuslb.ForeColor = System.Drawing.Color.Red;
statuslb.Width = 400;
statuslb.Height = 20;
statuslb.Visible = true;
panel.Controls.Add(statuslb);
//cancelbttn.Click += new EventHandler(cancelbttn_Click);
this.Controls.Add(panel);
}
catch (Exception ex)
{
statuslb.Text = "Contact with Administrator!";
}
}
public override void RenderControl(HtmlTextWriter writer)
{
panel.RenderControl(writer);
EnsureChildControls();
//create a table
//base.RenderControl(writer);
}
string str1;
public static string err;
void Checkpasswordbttn_Click(object sender, EventArgs e)
{
statuslb.Text = "";
if (userid_txt.Text != "" && oldpasswordtb.Text != "" && newpasswordtb.Text != "" && Confirmpasswordtb.Text != "")
{
////throw new Exception("The method or operation is not implemented.");
if (newpasswordtb.Text == Confirmpasswordtb.Text)
{
str1 = updatePassword(newpasswordtb.Text, oldpasswordtb.Text);
//if(Convert.ToInt64(str1) > 0)
if(str1=="1")
{
statuslb.Text = "your password has changed successfully!";
}
else
{
statuslb.Text = "Please check your oldpassword!";
}
}
else
{
statuslb.Text = "Newpassword and confrim password must be same!";
}
}
else
{
statuslb.Text = "All fields are manadatory!";
}
if(userid_txt.Text!="" && oldpasswordtb.Text!="")
{
bool verified = AuthenticateUser(userid_txt.Text, oldpasswordtb.Text);
if (verified)
statuslb.Text = "Verified";
else
statuslb.Text = "Not Verified" + err;
}
}
//public SqlConnection conn = new SqlConnection("Data Source=server name;Initial Catalog=databasename;Integrated Security=True");
public SqlConnection conn = new SqlConnection("Data Source=server name;Initial Catalog=databasename;Integrated Security=True");
public string updatePassword(string password, string oldpassword)
{
try
{
SPWeb thisWeb = SPControl.GetContextWeb(this.Context);
SPUser currentUser = thisWeb.CurrentUser;
String username = currentUser.Name;
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Update statement")";
return cmd.ExecuteNonQuery().ToString();
}
catch (Exception ex)
{
return ex.Message;
}


}
private string connectionstring;
// Property to Assign Site URL from ToolPart
[WebBrowsable(false)]
public string ConnString
{
get
{
return connectionstring;
}
set
{
connectionstring = value;
}
}
// Creating the ToolPart with the WebPart
#region IWebEditable Members
EditorPartCollection IWebEditable.CreateEditorParts()
{
List editors = new List();
editors.Add(new editchagepassword());
return new EditorPartCollection(editors);
}
object IWebEditable.WebBrowsableObject
{
get { return this; }
}


}
}

// Next Class
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace changepasswordwebpart
{
class editchagepassword:EditorPart
{
TextBox constringtxt;
public editchagepassword()
{
this.ID = "PasswordEditorPart";
this.Title = "Change Your Password";
}
protected override void CreateChildControls()
{
base.CreateChildControls();
constringtxt = new TextBox();
// Create new panel for ToolPart
//Panel userSectionBody = new Panel();
//constringtxt = new TextBox();
//Label lblSiteURL = new Label();
//lblSiteURL.Text = "Connection string";

//// Adding Site URL TextBox to Panel
//userSectionBody.Controls.Add(lblSiteURL);
//userSectionBody.Controls.Add(new LiteralControl("
"));
//userSectionBody.Controls.Add(constringtxt);


Controls.Add(constringtxt);
}
// This Function will be Triggered When the Apply or OK button Clicked in ToolPart
public override bool ApplyChanges()
{
changepasswordwebpart webpart = WebPartToEdit as changepasswordwebpart;
if (webpart != null)
{
webpart.ConnString = constringtxt.Text;
return true;
}
return true;
}
// This Function will be Triggered when the Task Pane of the WebPart is Opened
public override void SyncChanges()
{
EnsureChildControls();
changepasswordwebpart webpart = WebPartToEdit as changepasswordwebpart;
if (webpart != null)
{
constringtxt.Text = webpart.ConnString;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
// Rendering Tool Part Pane
//writer.WriteLine("Document Library TreeView Editor Part");
base.RenderContents(writer);
}
}
}