Login page with change password option in ASp.net

No.of Views3402
Bookmarked0 times
Downloads 
Votes0
By  Bharat   On  24 Nov 2010 02:11:24
Tag : ASP.NET , How to
Here i'll explain about how we can create login page in asp.net as well as how we can change password using asp.net
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

HTML CODE

  

<asp:Label ID="Label1" runat="server" Font-Size="X-Large" Text="Student Login"></asp:Label>

        <br />

 

        <br />

     

        <br />

        <br />

 

        <table style="width: 416px" >

            <tr>

                <td class="style1">

 

                    User ID:</td>

                <td>

                   

                   

                   

                    <asp:TextBox ID="user_txt" CssClass="watermarkOn" Text="UserName" runat="server"></asp:TextBox>

                    &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"

                        ControlToValidate="user_txt" ErrorMessage="Required" ValidationGroup="a"></asp:RequiredFieldValidator>

                </td>

            </tr>

            <tr>

                <td class="style1">

 Password:</td>

                <td>

                  

                    <asp:TextBox ID="pwd_txt" runat="server" CssClass="watermarkOn" Text="Password" TextMode="Password"></asp:TextBox>

                    &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"

                        ControlToValidate="pwd_txt" ErrorMessage="Required" ValidationGroup="a"></asp:RequiredFieldValidator>

                </td>

            </tr>

            <tr>

                <td class="style1">

                    &nbsp;</td>

                <td>

                 

                   

                 

                    <asp:Button ID="login_btn" runat="server" onclick="login_btn_Click"

                        Text="Login" ValidationGroup="a" Width="122px" />

                    </td>

            </tr>

            <tr>

                <td class="style1">

                    &nbsp;</td>

                <td>

                 

                   

                 

                    <asp:LinkButton ID="chg_btn" runat="server" onclick="chg_btn_Click"

                        ValidationGroup="t">Change

                    Password</asp:LinkButton>

                    </td>

            </tr>

            <tr>

                <td class="style1">

                   </td>

                <td>

          

                    <asp:Label ID="lbl_msg" runat="server" ForeColor="#FF3300"></asp:Label>

                      <br />

                      </td>

            </tr>

          

            </table>

   

    </div>

    <p>

 

        <table>

            <tr>

                <td>

                   

                    <asp:Panel ID="Panel1" runat="server" Visible="False" Height="309px"

                        Width="523px">

                  

                        <br />

                        <table>

                            <tr>

                                <td class="style3">

                                    

                                    <asp:LinkButton ID="close_lnk" runat="server" onclick="close_lnk_Click"

                                        ValidationGroup="z">Close</asp:LinkButton>

                                    </td>

                                <td class="style2">

                                  

                                    <asp:Label ID="lbl_msg2" runat="server"></asp:Label>

                                    </td>

                            </tr>

                            <tr>

                                <td class="style3">

                                   

                                    User ID:</td>

                                <td class="style2">

                             

                                 

                             

                                    <asp:TextBox ID="oldusr_txt" runat="server" Height="22px"></asp:TextBox>

                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"

                                        ControlToValidate="oldusr_txt" ErrorMessage="Required" ValidationGroup="c"></asp:RequiredFieldValidator>

                                </td>

                            </tr>

                            <tr>

                                <td class="style3">

                                   

                                    Old Password:&nbsp;</td>

                                <td class="style2">

                          

                                    <asp:TextBox ID="oldpwd_txt" runat="server" TextMode="Password"></asp:TextBox>

                                   <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"

                                        ControlToValidate="oldpwd_txt" ErrorMessage="Required" ValidationGroup="c"></asp:RequiredFieldValidator>

                                </td>

                            </tr>

                            <tr>

                                <td class="style3">

                                    </td>

                                <td class="style2">

                                    <asp:Button ID="btn_newpwd" runat="server" onclick="btn_newpwd_Click"

                                        Text="Confirm" ValidationGroup="c" Width="125px" />

                                    </td>

                            </tr>

                          

                            <tr>

                                <td class="style3">

                                   </td>

                                <td class="style2">

                                 

                                    <asp:Label ID="lbl_msg3" runat="server"></asp:Label>

                                    </td>

                            </tr>

                           <%-- <tr>

                                <td class="style4">

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                    User ID&nbsp;</td>

                                <td>

                                    &nbsp;&nbsp;

                                    <asp:TextBox ID="nusr_txt" runat="server" Enabled="False"></asp:TextBox>

                                    &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"

                                        ControlToValidate="npwd_txt" ErrorMessage="Required" ValidationGroup="c"></asp:RequiredFieldValidator>

                                </td>

                            </tr>--%>

                            <tr>

                                <td class="style3" >

                                     New

                                    Password&nbsp;</td>

                                <td class="style2">

                                  

                                    <asp:TextBox ID="npwd_txt" runat="server" Enabled="False" TextMode="Password"></asp:TextBox>

                                   <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"

                                        ControlToValidate="npwd_txt" ErrorMessage="Required" ValidationGroup="d"></asp:RequiredFieldValidator>

                                </td>

                            </tr>

                            <tr>

                                <td class="style3">

                                   

                                    Confirm Password</td>

                                <td class="style2">

                                

                                    <asp:TextBox ID="Conpwd_txt" runat="server" Enabled="False" TextMode="Password"></asp:TextBox>

                                    &nbsp;<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"

                                        ErrorMessage="Required" ControlToValidate="Conpwd_txt" ValidationGroup="d"> </asp:RequiredFieldValidator>

                                    &nbsp; <asp:CompareValidator ID="CompareValidator1" runat="server"

                                        ControlToCompare="npwd_txt" ControlToValidate="Conpwd_txt"

                                        ErrorMessage="Enter Same Password" ValidationGroup="d"></asp:CompareValidator>

                                    </td>

                            </tr>

                            <tr>

                                <td class="style3">

                                    &nbsp;</td>

                                <td class="style2">

                                 

                                    <asp:Button ID="chgpwd_btn" runat="server" Enabled="False"

                                        onclick="chgpwd_btn_Click" Text="Change Passord" ValidationGroup="d"

                                        Width="128px" />

                                 

                                    </td>

                            </tr>

                            <tr>

                                <td class="style3">

                                  </td>

                                <td class="style2">

                                   

                                    &nbsp;</td>

                            </tr>

                          

                        </table>

                       

                    </asp:Panel>

                 

                    </td>

            </tr>

        </table>

    </p>

    </form>

    <script type="text/javascript">

function WebForm_OnSubmit() {

if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) {

    for (var i in Page_Validators) {

        try {

            var control = document.getElementById(Page_Validators[i].controltovalidate);

            if (!Page_Validators[i].isvalid) {

                control.className = "ErrorControl";

            } else {

                control.className = "";

            }

        } catch (e) { }

    }

    return false;

}

return true;

}

</script>

/body>

</html>

 

 

C# code

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

 

public partial class MemberLogin : System.Web.UI.Page

{

    SqlConnection con = new SqlConnection();

    SqlDataAdapter adp;

    DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void login_btn_Click(object sender, EventArgs e)

    {

        con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;

        con.Open();

        adp = new SqlDataAdapter("select classid,sid,supass from tbStudentLogin where sid=@sid and supass=@supass", con);

        adp.SelectCommand.Parameters.Add("@sid", SqlDbType.VarChar, 50).Value = user_txt.Text;

        adp.SelectCommand.Parameters.Add("@supass", SqlDbType.VarChar, 50).Value = pwd_txt.Text;

        ds.Clear();

        adp.Fill(ds);

        adp.Dispose();

        if (ds.Tables[0].Rows.Count == 0)

        {

            lbl_msg.Text = "Invalid Userid or Password";

            user_txt.Text = "";

            pwd_txt.Text = "";

        }

        else

        {

            Response.Redirect("Nextpage.aspx");

            lbl_msg.Text = "";

 

        }

    }

    protected void chg_btn_Click(object sender, EventArgs e)

    {

        Panel1.Visible = true;

        user_txt.Enabled = false;

        pwd_txt.Enabled = false;

        login_btn.Enabled = false;

        chg_btn.Enabled = false;

        lbl_msg.Text = "";

        lbl_msg2.Text = "Enter Your Uid And Old Password";

  

    }

    protected void close_lnk_Click(object sender, EventArgs e)

    {

        Panel1.Visible = false;

        user_txt.Enabled = true;

        pwd_txt.Enabled = true;

        login_btn.Enabled = true;

        chg_btn.Enabled = true;

       

    }

    protected void btn_newpwd_Click(object sender, EventArgs e)

    {

        con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;

        con.Open();

        adp = new SqlDataAdapter("select sid,supass from tbStudentLogin where sid=@sid and supass=@supass", con);

        adp.SelectCommand.Parameters.Add("@sid", SqlDbType.VarChar, 50).Value = oldusr_txt.Text;

        adp.SelectCommand.Parameters.Add("@supass", SqlDbType.VarChar, 50).Value = oldpwd_txt.Text;

        ViewState["usrname"] = oldusr_txt.Text;

        ds.Clear();

        adp.Fill(ds);

        adp.Dispose();

        if (ds.Tables[0].Rows.Count == 0)

        {

            lbl_msg2.Text = "Invalid User";

        }

        else

        {

            oldpwd_txt.Enabled = false;

            oldusr_txt.Enabled = false;

            btn_newpwd.Enabled = false;

            //nusr_txt.Enabled = true;

            npwd_txt.Enabled = true;

            Conpwd_txt.Enabled = true;

            chgpwd_btn.Enabled = true;

            oldpwd_txt.Text = "";

            oldusr_txt.Text = "";

 

            lbl_msg2.Text = "";

            lbl_msg3.Text = "Enter Your New Password";

 

 

 

 

        }

    }

    protected void chgpwd_btn_Click(object sender, EventArgs e)

    {

        SqlConnection con = new SqlConnection();

        con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;

        con.Open();

        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "update tbStudentLogin  set supass=@supass where sid=@sid ";

        cmd.Connection = con;

        //cmd.Parameters.Add("@sid", SqlDbType.VarChar, 50).Value = nusr_txt.Text;

        cmd.Parameters.Add("@sid", SqlDbType.VarChar, 50).Value = ViewState["usrname"].ToString();

        cmd.Parameters.Add("@supass", SqlDbType.VarChar, 50).Value = npwd_txt.Text;

 

        cmd.ExecuteNonQuery();

        cmd.Dispose();

        Panel1.Visible = false;

        pwd_txt.Enabled = true;

        user_txt.Enabled = true;

        chg_btn.Enabled = true;

        login_btn.Enabled = true;

        lbl_msg.Text = "Your Password Has Been Changed";

        npwd_txt.Text = "";

        //nusr_txt.Text = "";

        lbl_msg2.Text = "";

        lbl_msg3.Text = "";

        Conpwd_txt.Text = "";

        oldpwd_txt.Enabled = true;

        oldusr_txt.Enabled = true;

 

        con.Close();

    }

}

 

SQL SCRIPT

 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tbStudentLogin]') AND type in (N'U'))

BEGIN

CREATE TABLE [dbo].[tbStudentLogin](

      [id] [int] IDENTITY(1,1) NOT NULL,

      [sid] [varchar](50) NULL,

      [supass] [varbinary](50) NULL,

 CONSTRAINT [PK_tbStudentLogin] PRIMARY KEY CLUSTERED

(

      [id] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]

END

 

 

 

 
Sign Up to vote for this article
 
About Author
 
Bharat
Occupation-Software Engineer
Company-Espranza
Member Type-Junior
Location-India
Joined date-13 Nov 2010
Home Page-
Blog Page-http://usingaspdotnet.blogspot.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</