Change a Password in ASP.NET

Introduction

The registered user needs to login with his/her login credentials (user name and password). After successful login a Change password link will be visible. Here by clicking the link a new page will appear where the user must enter the Current Password, New Password and Confirm Password and then click on the Update button to change his/her password respectively.

Table structure
password1.jpg
Now let's move to the coding part.

Default.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Password_Change_in_asp.net._Default" %>   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Untitled Page</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>     
  10.      <asp:Label ID="Label1" runat="server" Text="Name" Font-Bold="True"   
  11.             Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  12.         <asp:TextBox ID="TextBox_user_name" runat="server" ForeColor="#993300" Width="100px"></asp:TextBox><br />  
  13.         <asp:Label ID="Label2" runat="server" Text="Password" Font-Bold="True"   
  14.             Width="100px" BackColor="#FFFF66" ForeColor="#FF3300"></asp:Label>  
  15.         <asp:TextBox ID="TextBox_password" runat="server" ForeColor="#CC6600"   
  16.             TextMode="Password" Width="100px"></asp:TextBox><br />  
  17.         <asp:Button ID="btn_login" runat="server" Text="Login" Font-Bold="True"   
  18.             BackColor="#CCFF99" onclick="btn_login_Click"  /><br />  
  19.              <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label>  
  20.     </div>      
  21.     </form>  
  22. </body>  
  23. </html>

Default.aspx.cs

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14.   
  15. namespace Password_Change_in_asp.net  
  16. {  
  17.     public partial class _Default : System.Web.UI.Page  
  18.     {  
  19.         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  20.         string str = null;  
  21.         SqlCommand com;  
  22.         protected void btn_login_Click(object sender, EventArgs e)  
  23.         {  
  24.             object obj = null;  
  25.             SqlConnection con = new SqlConnection(strConnString);  
  26.             con.Open();  
  27.   
  28.             Session["UserName"] = TextBox_user_name.Text;  
  29.             str = "select count(*) from login where UserName=@UserName and Password =@Password";  
  30.             com = new SqlCommand(str, con);  
  31.             com.CommandType = CommandType.Text;  
  32.             com.Parameters.AddWithValue("@UserName", Session["UserName"]);  
  33.             com.Parameters.AddWithValue("@Password", TextBox_password.Text);  
  34.             obj = com.ExecuteScalar();  
  35.             if ((int)(obj) != 0)  
  36.             {  
  37.                 Response.Redirect("Welcome.aspx");  
  38.             }  
  39.             else  
  40.             {  
  41.                 lb1.Text = "Invalid Username and Password";  
  42.             }  
  43.             con.Close();  
  44.         }  
  45.     }  
  46. }
Welcome.aspx

 

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Welcome.aspx.cs" Inherits="Password_Change_in_asp.net.Welcome" %>   
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
  3. <html xmlns="http://www.w3.org/1999/xhtml" >  
  4. <head runat="server">  
  5.     <title>Untitled Page</title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.      <asp:Label ID="lb1" runat="server" Font-Bold="True" ForeColor="#FF3300"></asp:Label><br />  
  11.     </div>  
  12.     <asp:LinkButton ID="lnk_changepassword" runat="server"   
  13.         onclick="lnk_changepassword_Click">Change Password</asp:LinkButton>  
  14.     </form>  
  15. </body>  
  16. </html>
Welcome.aspx.cs
  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13.    
  14. namespace Password_Change_in_asp.net  
  15. {  
  16.     public partial class Welcome : System.Web.UI.Page  
  17.     {  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.             lb1.Text = "WELLCOME :: " + Session["UserName"];  
  21.         }  
  22.    
  23.         protected void lnk_changepassword_Click(object sender, EventArgs e)  
  24.         {  
  25.             Response.Redirect("Changepassword.aspx");  
  26.         }  
  27.     }  
  28. }
Changepassword.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Changepassword.aspx.cs"   
  2. Inherits="Password_Change_in_asp.net.Changepassword" %>   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml" >  
  5. <head runat="server">  
  6.     <title>Untitled Page</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>   
  11.         <asp:Label ID="Label1" runat="server" Text="Current password" Width="120px"   
  12.             Font-Bold="True" ForeColor="#996633"></asp:Label>  
  13.         <asp:TextBox ID="txt_cpassword" runat="server" TextMode="Password"></asp:TextBox>  
  14.         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"   
  15.             ControlToValidate="txt_cpassword"   
  16.             ErrorMessage="Please enter Current password"></asp:RequiredFieldValidator>  
  17.         <br />  
  18.          <asp:Label ID="Label2" runat="server" Text="New password" Width="120px"   
  19.             Font-Bold="True" ForeColor="#996633"></asp:Label>  
  20.         <asp:TextBox ID="txt_npassword" runat="server" TextMode="Password"></asp:TextBox>  
  21.         <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"   
  22.             ControlToValidate="txt_npassword" ErrorMessage="Please enter New password"></asp:RequiredFieldValidator>  
  23.         <br />  
  24.           
  25.          <asp:Label ID="Label3" runat="server" Text="Confirm password" Width="120px"   
  26.             Font-Bold="True" ForeColor="#996633"></asp:Label>  
  27.   
  28.         <asp:TextBox ID="txt_ccpassword" runat="server" TextMode="Password"></asp:TextBox>     
  29.   
  30.         <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"   
  31.             ControlToValidate="txt_ccpassword"   
  32.             ErrorMessage="Please enter Confirm  password"></asp:RequiredFieldValidator>  
  33.   
  34.         <asp:CompareValidator ID="CompareValidator1" runat="server"   
  35.             ControlToCompare="txt_npassword" ControlToValidate="txt_ccpassword"   
  36.             ErrorMessage="Password Mismatch"></asp:CompareValidator>      
  37.     </div>  
  38.     <asp:Button ID="btn_update" runat="server" Font-Bold="True" BackColor="#CCFF99" onclick="btn_update_Click" Text="Update" />  
  39.     <asp:Label ID="lbl_msg" Font-Bold="True" BackColor="#FFFF66" ForeColor="#FF3300" runat="server" Text=""></asp:Label><br />  
  40.     <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Login</asp:HyperLink>  
  41.     </form>  
  42. </body>  
  43. </html>
Changepassword.aspx.cs

 

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13. using System.Data.SqlClient;  
  14.   
  15. namespace Password_Change_in_asp.net  
  16. {  
  17.     public partial class Changepassword : System.Web.UI.Page  
  18.     {  
  19.         string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  20.         string str = null;  
  21.         SqlCommand com;  
  22.         byte up;  
  23.         protected void btn_update_Click(object sender, EventArgs e)  
  24.         {  
  25.             SqlConnection con = new SqlConnection(strConnString);  
  26.             con.Open();  
  27.             str = "select * from login ";  
  28.             com = new SqlCommand(str, con);  
  29.             SqlDataReader reader = com.ExecuteReader();  
  30.             while (reader.Read())  
  31.             {  
  32.                 if (txt_cpassword.Text == reader["Password"].ToString())  
  33.                 {  
  34.                     up = 1;  
  35.                 }  
  36.             }  
  37.             reader.Close();  
  38.             con.Close();  
  39.             if (up == 1)  
  40.             {  
  41.                 con.Open();  
  42.                 str = "update login set Password=@Password where UserName='" + Session["UserName"].ToString()+ "'";  
  43.                 com = new SqlCommand(str, con);  
  44.                 com.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar, 50));  
  45.                 com.Parameters["@Password"].Value = txt_npassword.Text;  
  46.                 com.ExecuteNonQuery();  
  47.                 con.Close();  
  48.                 lbl_msg.Text = "Password changed Successfully";  
  49.             }  
  50.             else  
  51.             {  
  52.                 lbl_msg.Text = "Please enter correct Current password";  
  53.             }  
  54.         }   
  55.     }  
  56. }
Output

Providing credentials (user name and password) for login:

password2.jpg

After successful login:

password3.jpg

Changing the current password by providing a new password:

password4.jpg

Password changed successfully:

password5.jpg
Conclusion

So here we learned how to change a password in ASP.Net.


Similar Articles