How to find Whether Cookie is present in web browser or not using c#

By
Today,i am going to explain "How to find Cookie is present in browser or not".If cookie is present in browser then you can login your asp.net application using cookie file.Many times you have seen your GoogleFacebook,Microsoft and other accounts easily login without username and password.what is the reason about that? Your username and password already stored in browser memory (Cookie file).Here i am going to explain "how to implement cookie based login in asp.net application". In this tutorial ,i have mainly focused cookie is present in browser or not.If you want to learn basic fundamentals of cookies here. In this application i have used session variable also to transfer information from one page to another page. 
There are some steps to implement this concepts in asp.net application which are given below:-
  StepFirst open your visual studio --> File-->New-->website-->Select ASP.NET Empty website--> OK --> open solution Explorer --> Add a web form (home.aspx) --> drag and drop label ,Text box  and button controls on the form as shown below:-


login_form

 Step 2 Now ope solution Explorer-->add Database.mdf file --> create two table Admin and Student as shown below:-


table

Note:- if you are facing problem to add .mdf file on site then read below links:-
 Step 3 Now open solution Explorer -->Add a New web form (welcome.aspx) -->Drag and drop Grid View, Label and Button controls on the form and configuration it as shown below:-


gridview

Step 4 Now Open home.aspx page --> write the following c# codes on submit button clicks (home.aspx.cs page) as given below:-
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True;");
        con.Open();
        if (Request.Cookies.Keys.Count == 0)
        { 
            SqlCommand cmd = new SqlCommand("select COUNT(*)FROM admin WHERE username='" + TextBox1.Text + "' and password='" + TextBox2.Text + "'");
            cmd.Connection = con;
            string usr = TextBox1.Text;
            string pass = TextBox2.Text;
            HttpCookie mycookie = new HttpCookie("usr", "pass");
            Response.Cookies.Add(mycookie);
            Session["cookies"] = "No Cookie is found in your web Browser";
            int obj = Convert.ToInt32(cmd.ExecuteScalar());
            if (obj > 0)
            {
                
                Response.Redirect("welcome.aspx");    
            }
            else
            {
                Response.Redirect("home.aspx");
                Label1.Text = "Invalid User Name or Password";
            }
        }
        else
        {
            if (Request.Cookies.Keys.Count == 1)
            {
                Session["cookies"] = "One Cookie is found in your web Browser";
                Response.Redirect("welcome.aspx");   
            }
            else
            {
       Session["cookies"] = "More than one Cookie is found in your web Browser";
                Response.Redirect("welcome.aspx");  
            }
        }
    }
}

Step 5 Now open welcome.aspx page-->write the following c# codes in welcome.aspx.cs file as given below:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["cookies"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["cookies"] = null;
        Response.Redirect("home.aspx");
    }
}

Step 6 Now Run the application (press F5) --> Enter username and password ( ram, ram123)--> you will see following output as shown below:-


cookie_output

Step 7 when you login with username and password again then you will see following output which are shown below:-


Step 8 Now copy your web browser url, which is shown above image --> Now open new tab of your web browser --> Paste the url http://localhost:52516/WebSite37/welcome.aspx) --> you will see same image as shown in above step 6 and step 7 because that page is stored in browser's cookie.

Description:- 
  1. When any user login in application at first time then it will show "No cookie is found in your web browser".
  2. When user login second time with different username or same  then it will show "One cookie is found in your web browser".
  3. When user login more than one user name then it will show "More than one cookie is found in your web browser".
For More...
  1. How to use wcf services in asp.net application
  2. How to use Form based authentication in asp.net application
  3. How to create generic handler application in asp.net
  4. How to use navigation control in asp.net application
  5. How to use web forms controls in asp.net website
  6. E-POST System project in asp.net
  7. How to implement Reflection in c#
  8. How to add Run time controls in .Net
  9. How to use File Handling concepts in .net application
  10. How to implement generic collections in application
  11. Difference between stored procedure and function
  12. Real Ado.net application
  13. How to copy data from one table to another table easily
  14. How to use multiple main method in c#
  15. How to implement data integrity in sql server
  16. How to convert type in c# (type conversion)
You can directly run this application and use it in your existing application.
                 Download whole attached file
                    Download

0 comments:

Post a Comment

Powered by Blogger.