Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

How to Check if cookies are enabled or disabled - Part 61

Suggested Videos
Part 58 - Techniques to send data from one webform to another
Part 59 - QueryString in asp.net
Part 60 - Cookies in asp.net

Different techniques to send data from one webform to another 
1. Cross Page Postback: Discussed in Part 55 and Part 56
2. Context.Handler object - Discussed in Part 58
3. Query strings - Discussed in Part 59
4. Cookies - Discussed in Part 60

The following concepts will be discussed in the subsequent sessions
5. Session state
6. Application state



How to Check if cookies are enabled or disabled in asp.net? This is what we will discuss in this video session. Most of the articles on the internet, states we can use Request.Browser.Cookies property to check, if the cookies are enabled or disabled. This is incorrect.
if (Request.Browser.Cookies)
{
    //Cookies Enabled
}
else
{
    //Cookies Disabled
}

Request.Browser.Cookies property is used to check, if the browser supports cookies. Most mordern browsers, support cookies. Irrespective of whether, the cookies are enabled or disabled, if the browser supports cookies, Request.Browser.Cookies always returns true. So use this property to check if the browser supports cookies and not to check if the cookies are enabled or disabled.
if (Request.Browser.Cookies)
{
    //Broswer supports cookies
}
else
{
    //Broswer does not supports cookies
}



So, the next question is, how do we check, if cookies are enabled or disabled? 
1. Write a Test Cookie
2. Redirect to the same page
3. Read the Test Cookie
4. If Cookies preseent - Cookies are enabled
5. Else - Cookies are disabled.

To disable cookies in Internet Explorer(IE 9)
1. Click on Tools
2. Select Internet Options
3. Click on the Privacy tab
4. Click the Advanced button, under settings
5. Check Override automatics cookie handling check box
6. Select Block radio button under First Party cookies and Third Party Cookie

The above steps disable cookies, only for the internet zone. If you are testing code on your local machine, and to disable cookies for localhost
1. Run the application
2. Press F12, to open developer tools
3. Then Select, Cache - Disable Cookies

WebForm1.aspx HTML source:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>
                This is WebForm1</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:TextBox ID="txtName" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:TextBox ID="txtEmail" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2"> 
            <asp:Button ID="btnSendData" runat="server" 
            Text="Go to WebForm2" onclick="btnSendData_Click" />
        </td>
    </tr>
    <tr>
        <td colspan="2"> 
            <asp:Label ID="lblMessage" runat="server"
            ForeColor="Red" Font-Bold="true">
            </asp:Label>
        </td>
    </tr>
</table>
</div>

WebForm1.aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Check if the browser supports cookies
        if (Request.Browser.Cookies)
        {
            if (Request.QueryString["CheckCookie"] == null)
            {
                // Create the test cookie object
                HttpCookie cookie = new HttpCookie("TestCookie", "1");
                Response.Cookies.Add(cookie);
                // Redirect to the same webform
                Response.Redirect("WebForm1.aspx?CheckCookie=1");
            }
            else
            {
                //Check the existence of the test cookie
                HttpCookie cookie = Request.Cookies["TestCookie"];
                if (cookie == null)
                {
                    lblMessage.Text = "We have detected that, the cookies are disabled on your browser. Please enable cookies.";
                }
            }
        }
        else
        {
            lblMessage.Text = "Browser doesn't support cookies. Please install one of the modern browser's that support cookies.";
        }
    }
}

protected void btnSendData_Click(object sender, EventArgs e)
{
    // Create the cookie object
    HttpCookie cookie = new HttpCookie("UserDetails");
    cookie["Name"] = txtName.Text;
    cookie["Email"] = txtEmail.Text;
    // Cookie will be persisted for 30 days
    //cookie.Expires = DateTime.Now.AddDays(30);
    // Add the cookie to the client machine
    Response.Cookies.Add(cookie);

    Response.Redirect("WebForm2.aspx");
}

WebForm2.aspx HTML Source:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>This is WebForm2</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:Label ID="lblName" runat="server">
            </asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:Label ID="lblEmail" runat="server">
            </asp:Label>
        </td>
    </tr>
</table>
</div>

WebForm2.aspx.cs Code
protected void Page_Load(object sender, EventArgs e)
{
    HttpCookie cookie = Request.Cookies["UserDetails"];
    if (cookie != null)
    {
        lblName.Text = cookie["Name"];
        lblEmail.Text = cookie["Email"];
    }
}

1 comment:

  1. Hi Venkat am a computer science student. thanks for the effort u hav taken for the tutorials.

    From the above code why there is a need of

    Response.Redirect("WebForm1.aspx?CheckCookie=1"); Redirect to the same webform.

    Is it not possible to Check the existence of the test cookie in the same page without redirecting. it will be help ful if we get response on this query.. thanks in advance

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.