Thursday, 23 May 2013

Print Working User Registration , Login and Forget Password in asp.net


User Registration , Login and Forget Password :
.aspx Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UserRegistration.aspx.cs" Inherits="Login_And_Registration_UserRegistration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>User Registration</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table>
    <tr>
     <td>First Name:</td>
     <td><asp:TextBox ID="txtFirstName" runat="server">
        </asp:TextBox>
     </td>
     <td><asp:RequiredFieldValidator ID="rfvFirstName"
                 runat="server"
                 ControlToValidate="txtFirstName"
                ErrorMessage="First Name can't be left blank"
                SetFocusOnError="True">*
         </asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td>Last Name:</td>
    <td><asp:TextBox ID="txtLastName" runat="server">
        </asp:TextBox></td>
    <td><asp:RequiredFieldValidator
             ID="RequiredFieldValidator1" runat="server"
             ControlToValidate="txtLastName"
             ErrorMessage="Last Name can't be left blank"
             SetFocusOnError="True">*
        </asp:RequiredFieldValidator>
    </td></tr>
    
    <tr><td>UserName:</td>
    <td><asp:TextBox ID="txtUserName" runat="server">
        </asp:TextBox>
    </td>
    <td><asp:RequiredFieldValidator
             ID="rfvUserName"
             runat="server"
             ControlToValidate="txtUserName"
             ErrorMessage="UserName can't be left blank"
             SetFocusOnError="True">*
        </asp:RequiredFieldValidator>
    </td></tr>
    
    <tr><td>Password:</td>
    <td><asp:TextBox ID="txtPwd" runat="server"
                     TextMode="Password">
        </asp:TextBox>
    </td>
    <td><asp:RequiredFieldValidator ID="rfvPwd"
             runat="server" ControlToValidate="txtPwd"
             ErrorMessage="Password can't be left blank"
             SetFocusOnError="True">*
       </asp:RequiredFieldValidator>
    </td></tr>
    
    <tr><td>Confirm Password:</td>
    <td><asp:TextBox ID="txtRePwd" runat="server"
                     TextMode="Password">
        </asp:TextBox>
    </td>
    <td><asp:CompareValidator ID="CompareValidator1"
             runat="server"
             ControlToCompare="txtRePwd"
             ControlToValidate="txtPwd"
             Operator="Equal"
             ErrorMessage="Password and confirm password do not match"
             SetFocusOnError="True">
        </asp:CompareValidator>
    </td></tr>
    
    <tr><td>Gender:</td>
    <td><asp:RadioButtonList ID="rdoGender"
                             runat="server">
             <asp:ListItem>Male</asp:ListItem>
             <asp:ListItem>Female</asp:ListItem>
        </asp:RadioButtonList>
    </td>
    <td><asp:RequiredFieldValidator
             ID="RequiredFieldValidator2"
             runat="server"
             ControlToValidate="rdoGender"
             ErrorMessage="Gender can't be left blank"
             SetFocusOnError="True">*
         </asp:RequiredFieldValidator>
    </td></tr>
    
    <tr><td>Address:</td>
    <td><asp:TextBox ID="txtAdress" runat="server"
                     TextMode="MultiLine">
        </asp:TextBox>
    </td>
    <td><asp:RequiredFieldValidator ID="rfvAddress"
             runat="server"
             ControlToValidate="txtAdress"
             ErrorMessage="Address can't be left blank"
             SetFocusOnError="True">*
        </asp:RequiredFieldValidator>
    </td></tr>
                           
    <tr><td>Email ID:</td>
    <td><asp:TextBox ID="txtEmailID" runat="server">
        </asp:TextBox>
    </td>
   <td><asp:RequiredFieldValidator
           ID="RequiredFieldValidator3"
           runat="server"
           ControlToValidate="txtEmailID"
           ErrorMessage="Email can't be left blank"
           SetFocusOnError="True">*
     </asp:RequiredFieldValidator>
   </td></tr>
   
   <tr><td><asp:Label ID="lblMsg" runat="server">
           </asp:Label>
       </td>
   <td><asp:ValidationSummary ID="ValidationSummary1"
            runat="server" ShowMessageBox="True"
            ShowSummary="False"/>
   </td></tr>
   
   <tr><td><asp:Button ID="btnSave" runat="server"
                       Text="Sign Up"
                       onclick="btnSave_Click"/>
   </td></tr>
   </table>
    </div>
    </form>
</body>
</html>

.CS Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Login_And_Registration_UserRegistration : System.Web.UI.Page
{
    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string insertSql = "INSERT INTO Users (FirstName,LastName,UserName,Password,Email,Address,Gender)"
       + " values (@FirstName,@LastName,@UserName,@Password,@Email,@Address,@Gender)";
        //Create SQL connection
        SqlConnection con = new SqlConnection(connectionString);

        //Create SQL Command And Sql Parameters

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;

        cmd.CommandType = CommandType.Text;
        cmd.CommandText = insertSql;

        SqlParameter firstName = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
        firstName.Value = txtFirstName.Text.ToString();
        cmd.Parameters.Add(firstName);

        SqlParameter lastName = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
        lastName.Value = txtLastName.Text.ToString();
        cmd.Parameters.Add(lastName);

        SqlParameter userName = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
        userName.Value = txtUserName.Text.ToString();
        cmd.Parameters.Add(userName);

        SqlParameter pwd = new SqlParameter("@Password", SqlDbType.VarChar, 50);
        pwd.Value = txtPwd.Text.ToString();
        cmd.Parameters.Add(pwd);

        SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
        email.Value = txtEmailID.Text.ToString();
        cmd.Parameters.Add(email);

        SqlParameter address = new SqlParameter("@Address", SqlDbType.VarChar, 50);
        address.Value = txtAdress.Text.ToString();
        cmd.Parameters.Add(address);

        SqlParameter gender = new SqlParameter("@Gender", SqlDbType.VarChar, 10);
        gender.Value = rdoGender.SelectedItem.ToString();
        cmd.Parameters.Add(gender);


        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            lblMsg.Text = "User Registration successful";
        }
        catch (SqlException ex)
        {
            string errorMessage = "Error in registering user";
            errorMessage += ex.Message;
            throw new Exception(errorMessage);

        }
        finally
        {
            con.Close();
        }
    }
}

Login .aspx Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login_And_Registration_Login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Name" runat="server" Text="UserName:" CssClass="lbl"/>
<br/>
<asp:TextBox ID="txtUserName" runat="server" Height="22px"/>
<asp:RequiredFieldValidator ID="RV1" runat="server"
                            ControlToValidate="txtUserName"
                            ErrorMessage="Please Enter User Name"
                            SetFocusOnError="True">*
</asp:RequiredFieldValidator><br />
</div>

<div class='container'>
<asp:Label ID="lblPwd" runat="server" Text="Password:" CssClass="lbl"/>
<br/>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"
                         CssClass="pwd" Height="22px"/>
<asp:RequiredFieldValidator ID="RV2" runat="server"
                            ControlToValidate="txtPwd"
                            ErrorMessage="Your Password"
                            SetFocusOnError="True">*
</asp:RequiredFieldValidator><br />
</div>

<div class='container'>
<asp:Button ID="btnLogIn" runat="server" Text="Sign In"
                          onclick="btnLogIn_Click"/>
</div>
                      
<div class='container'>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="~/Login And Registration/ForgotPassword.aspx">Forgot Password ?</asp:HyperLink>
<br/>
</div>
<div class='short_explanation'>New User ?
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl="~/Login And Registration/UserRegistration.aspx">SignUp !</asp:HyperLink></div>                      
<asp:ValidationSummary ID="ValidationSummary1"
                       runat="server" CssClass="error"/>
<br /><br />
<asp:Label ID="lblMsg" runat="server" Text="" CssClass="lbl"/>
</fieldset>
    </div>
    </form>
</body>
</html>

Login .CS CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Login_And_Registration_Login : System.Web.UI.Page
{
    string strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogIn_Click(object sender, EventArgs e)
    {
        //Create Connection String And SQL Statement
      
        string strSelect = "SELECT COUNT(*) FROM Users WHERE UserName = @Username AND Password = @Password";

        SqlConnection con = new SqlConnection(strCon);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strSelect;

        SqlParameter username = new SqlParameter("@Username", SqlDbType.VarChar, 50);
        username.Value = txtUserName.Text.Trim().ToString();
        cmd.Parameters.Add(username);

        SqlParameter password = new SqlParameter("@Password", SqlDbType.VarChar, 50);
        password.Value = txtPwd.Text.Trim().ToString();
        cmd.Parameters.Add(password);

        con.Open();
        int result = (Int32)cmd.ExecuteScalar();
        con.Close();

        if (result >= 1)
        {
            Response.Redirect("UpdateProfile.aspx");
        }
        else
            lblMsg.Text = "Incorrect Username or Password";
       

    }
}


ForgetPassword .aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ForgotPassword.aspx.cs" Inherits="Login_And_Registration_ForgotPassword" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ForgotPassword</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <div>
     <fieldset>
     <legend>Forgot Password</legend>
     <asp:Label ID="lblEmail" runat="server" Text="Email Address: "/>
     <asp:TextBox ID="txtEmail" runat="server"/>
     
     <asp:RequiredFieldValidator ID="RV1" runat="server"
                                 ControlToValidate="txtEmail"
                                ErrorMessage="Please Enter EmailID"
                                SetFocusOnError="True">*
    </asp:RequiredFieldValidator>
    
    <asp:Button ID="btnPass" runat="server" Text="Submit"
                             onclick="btnPass_Click"/>
    
    <asp:ValidationSummary ID="ValidationSummary1"
                          runat="server" CssClass="error"/>
                          
    <asp:Label ID="lblMessage" runat="server" Text=""/>
    </fieldset>
    </div>
    </form>
</body>
</html>

Forget Password .CS CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Mail;

public partial class Login_And_Registration_ForgotPassword : System.Web.UI.Page
{
     string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnPass_Click(object sender, EventArgs e)
    {
        //Create Connection String And SQL Statement
      
        string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email";

        SqlConnection connection = new SqlConnection(strConnection);
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.Text;
        command.CommandText = strSelect;

        SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50);
        email.Value = txtEmail.Text.Trim().ToString();
        command.Parameters.Add(email);

        //Create Dataset to store results and DataAdapter to fill Dataset
        DataSet dsPwd = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter(command);
        connection.Open();
        dAdapter.Fill(dsPwd);
        connection.Close();
        if(dsPwd.Tables[0].Rows.Count > 0 )
        {
            MailMessage loginInfo = new MailMessage();
            loginInfo.To.Add(txtEmail.Text.ToString());
            loginInfo.From = new MailAddress("YourID@gmail.com");
            loginInfo.Subject = "Forgot Password Information";

            loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "";
            loginInfo.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword");
            smtp.Send(loginInfo);
            lblMessage.Text = "Password is sent to you email id,you can now Login";
        }
        else
        {
            lblMessage.Text = "Email Address Not Registered";
        }

    }
}

No comments:

Post a Comment