Thursday, 19 February 2015

How To Make Registration And Login Page In ASP.NET



Hi Friends!Today, i am going to make a Custom  Registration and Login Page.This is not a simple web form page.In this Form i have used many concepts.You can easily implement this concept to any where in .NET Application. In this application i have covered all things which is required in various Registration and Login Form in any  website.I am really saying to you,if you run this application on your computer then you will feel how much good it is.You can download this application from bottom and run on your visual studio.There are some controls which i have used in this application.
There are some steps to make this application.Please follow this.
Step 1: First open your visual studio->File->New->website->ASP.NET Empty website->click OK.->Now open Solution Explorer->Add New Item-> Web form-> click Add.
Step 2: Now make a Registration page like this,which is given below.
see it:
Make this Registration Form in following ways:
  • First put ScriptManager on your form.
  • Create UserName-->Now put UpdatePanel-->Now put TextBox in UpdatePanel->Put RequiredFieldValidator.
  • Password--> TextBox-->Now put RequiredFieldValidator.
  • Retype Password-->TextBox-->Now put RequiredFieldValidator.
  • Mobile Number-->TextBox-->Put RegularExpressionValidator &  RequiredFieldValidator
  • Email Id -->TextBox-->Put RegularExpressionValidator and  RequiredFieldValidator.
  • Captcha Image-->To know more here
  • Enter Captcha Image-->TextBox-->Put Label and RequiredFieldValidator.
Now go propeties in every RegularExpressionValidator &  RequiredFieldValidator and set following fields which is given below:
Control To Validate -->Select TextBox which you want to validate. open this application on your visual studio and see all changes.This is more easy for you.
Step 3: Now Add Database(.mdf) on your website.Open Solution Explorer -->Right click on website-->Add New Item-->Sql Server Database-->click Add.

Now if you are facing problem in adding database(.mdf) on Website,please here.
Step 4: Now Double click  on Database.mdf --> Solution Exporer will open-->Right click on Tables -->Add New Table-->Now Enter the column name.
see it:
Step 5: Now double click on Submit Button and write the following code which is given below:
01using System;
02using System.Web;
03using System.Web.UI;
04using System.Web.UI.WebControls;
05using System.Data.SqlClient;
06using System.Drawing;
07 
08public partial class _Default : System.Web.UI.Page
09{
10    protected void TextBox1_TextChanged(object sender, EventArgs e)
11    
12        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
13        con.Open();
14        SqlCommand cmd = new SqlCommand("select*from regform where username='" + TextBox1.Text + "'", con);
15        SqlDataReader dr = cmd.ExecuteReader();
16 
17        if (dr.Read())
18        {
19            Label1.Text = "User Name is Already Exist";
20            this.Label1.ForeColor = Color.Red;
21        }
22        else
23        {
24            Label1.Text = "UserName is Available";
25            this.Label1.ForeColor = Color.Red;
26        }
27        con.Close();
28    }
29    protected void  Button1_Click(object sender, EventArgs e)
30{
31    captcha1.ValidateCaptcha(TextBox6.Text.Trim());
32    if (captcha1.UserValidated)
33    {   //you can use disconnected architecture also,here i have used connected architecture.
34        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
35        con.Open();
36       SqlCommand cmd = new SqlCommand("insert into regform values(@a,@b,@c,@d)",con);
37        cmd.Parameters.AddWithValue("a", TextBox1.Text);
38        cmd.Parameters.AddWithValue("b", TextBox2.Text);
39        cmd.Parameters.AddWithValue("c", TextBox4.Text);
40        cmd.Parameters.AddWithValue("d", TextBox5.Text);
41        cmd.ExecuteNonQuery();
42        Session["name"] = TextBox1.Text;
43        Response.Redirect("default.aspx");
44        con.Close();
45    }
46    else
47    {
48 Label2.ForeColor = System.Drawing.Color.Red;
49 Label2.Text = "You have Entered InValid Captcha Characters please Enter again";
50  }    
51 }  
52 }

Step 6: Now create a New page and make a Login form which is give below:
see it:
Note->In this application  all (* )represents the Label control.
Step 7: Now Double click on Login Button and write the following codes which is given below:
01using System;
02using System.Web;
03using System.Web.UI;
04using System.Web.UI.WebControls;
05using System.Data.SqlClient;
06using System.Drawing;
07 
08public partial class login : System.Web.UI.Page
09{
10    protected void Button1_Click(object sender, EventArgs e)
11    {
12        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
13        con.Open();
14        SqlCommand cmd = new SqlCommand("select COUNT(*)FROM regform WHERE username='"+ TextBox1.Text + "' and password='" + TextBox2.Text + "'");
15        cmd.Connection = con;
16        int OBJ = Convert.ToInt32(cmd.ExecuteScalar());
17        if (OBJ > 0)
18        {
19            Session["name"] = TextBox1.Text;
20            Response.Redirect("default.aspx");
21        }
22        else
23        {
24            Label1.Text = "Invalid username or password";
25            this.Label1.ForeColor = Color.Red;
26        }
27    }
28    protected void LinkButton2_Click(object sender, EventArgs e)
29    {
30        Response.Redirect("Registration.aspx");
31    }
32}

Step 8: Now Create a another page (Default.aspx)which is given below:
see it:

Step 9: Now Double click on Page and write the following codes  on Page load which is given below:

01using System;
02using System.Web;
03using System.Web.UI;
04using System.Web.UI.WebControls;
05 
06public partial class _Default : System.Web.UI.Page
07{
08    protected void Page_Load(object sender, EventArgs e)
09    {
10        Label1.Text = Session["name"].ToString();
11    }
12}

Step 10: Now Run the program(Press F5).
see it:

There are some points to see the Exact output of whole application.
     1. If you are not part of this application then click New User Button.
   see it:
   

     2. When you will Enter UserName then this user name will check from database.if this name is available then it will show User Name is available otherwise show User Name is Already Exist.
  see it:

     3.  when you will Enter Password & ReType Password  Field ,if both Password are not matched then see it:
    
      4. when you will Enter Mobile Number Field ,if Number is Not correct (according to India).you will see following output:
  
  
      5If you will Enter Email Id Field and Email is not correct format then you will see following error.

     6.when  you will enter wrong Captcha code then you will see following output:

  

     7. When you will enter all fields in Registration page correct then you will see following output:
  
 Step 11: Now you will see Rajesh has  entered Database(.mdf).
see student table:

  
   Step 12: Now you can directly Login  with correct Login Name and Password,which is present inDatabase(.mdf).
see it:

see output:-
Step 13 : You can add Change Password page in this page from  here

Step 14 : You can add Forget password codes in this page from here

Step 15 : You can add send email service in this page from here

Step 16 : You can build 3 Tier Registration and Login page from here.

Step 17 : You can create existing asp.net registration and login page esaily from here.

Note- If you want to run this application on your system directly, without any changes then follow first two steps(1 to 2)  and other four steps(3 to 6),you can add extra features in your application which are given below:
  1. First download this application form bottom and open this application to your visual studio 2010.
  2. Now go Tools -->Options -->Database Tools -->Data connections -->Remove Sql Server Instance Name(blank for default) from right hand side-->click OK.
  3. If you want ,you can implement Form Based authentication in this form for security purpose.
  4. You can use different connection strings for your registration and loin page also from here. Such as store procedure,connected architecture,disconnected architecture parameterized connection etc.It will provide full security to your asp.net application.
  5. This registration page is secure,i have putted code security also.

No comments:

Post a Comment