Cookies,how to make Cookies in asp.net with C#.

 What are Cookies, Make Cookies in asp.net with C#:



Cookies:

  • Cookies are small part of information generated by a Web server.
  • Cookies stored in the user's computer (Local Machine).
  • Cookies use for future access.
  • Cookies are embedded in the HTML information flowing back and forth.
  • Shear information between the user's computer (Local Machine) and the servers.
  • Cookies allow user-side customization of Web information.
For example, cookies are used to personalize Web search engines (information).
Many organizations and companies use ``cookies'' to track your every move on their site. Cookies, Session, and Application object are in Asp.net.

Advantages of Cookies:

We consider following advantages of using cookies in a web application.
  • It's very simple to use and implement.
  • Browser takes care of sending the data.
  • The browser automatically arranges them.

Disadvantages of Cookies:

Consider following disadvantages of cookies are
  • It stores data in simple text format, so it's not secure at all.
  • There is a size limit for cookies data (4096 bytes / 4KB).
  • The maximum number of cookies allowed is also limited.
  •  Most browsers provide limits the number of cookies to 20.
  • We need to configure the browser.
Now we create a web page and make cookies and save in browser.

How to create Cookies in asp.net:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Cookies_Demo.Default" %>

<!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></title>
    <style type="text/css">
        .style1
        {
            color: #990000;
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <h1 class="style1">
            How to Add Cookies Demo</h1>
        <div>
            <table style="height: 153px; background-color: #C0C0C0">
                <tr>
                    <td align="right" style="font-weight: 700">
                        Enter E-Mail Id or User Name :<br />
                        &nbsp;<br />
                        Enter Password :
                        <br />
                        <br />
                        <asp:CheckBox ID="CheckBox1" runat="server" Text="Remember me" />
                    </td>
                    <td align="left">
                        <asp:TextBox ID="TextBox1" runat="server" placeholder="Email" Width="198px"></asp:TextBox>
                        <br />
                        <br />
                        <asp:TextBox ID="TextBox2" runat="server" placeholder="Password" TextMode="Password"
                            Width="196px"></asp:TextBox>
                        <br />
                        <br />
                        <asp:Button ID="Button2" runat="server" Text="Login" OnClick="Button1_Click" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html> 


How to create Cookies in asp.net by C#:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cookies_Demo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            /* Checking for cookies */
            if (!IsPostBack)
            {
                if (HttpContext.Current.Request.Cookies["UserInfodemo"] != null)
                {
                    string email = Request.Cookies["UserInfoDemo"]["EmailDemo"].ToString();
                    TextBox1.Attributes.Add("value", email);

                    string pass = Request.Cookies["UserInfoDemo"]["PasswordDemo"].ToString();
                    TextBox2.Attributes.Add("value",pass);
                }
                else if (HttpContext.Current.Request.Cookies["UserInfoDemo"] == null)
                {
                    TextBox1.Text = "";
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            /* if login successfull */
            if (CheckBox1.Checked == true)
            {
                /* Store Cookies into browser here at login time */
                /* Place into Coockies */
                //Creting a Cookie Object
                HttpCookie _userInfoCookies = new HttpCookie("UserInfoDemo");

                //Setting values inside it
                _userInfoCookies["EmailDemo"] = TextBox1.Text.Trim();
                _userInfoCookies["PasswordDemo"] = TextBox2.Text.Trim();
                //Adding Expire Time of cookies
                _userInfoCookies.Expires = DateTime.Now.AddDays(10);

                //Adding cookies to current web response
                Response.Cookies.Add(_userInfoCookies);
            }

            /* do your operation */
        }
    }
}

Cookies,how to make Cookies in asp.net with C#.



Other Asp.net related Post:


Comments

  1. cookies are used to personalize Web search engines (information).
    Many organizations and companies use ``cookies'' to track your every move on their site. Cookies, Session, and Application object are in Asp.net.Thanks for your informative article. Dot Net Training in chennai | dot net course fees

    ReplyDelete

Post a Comment

Popular posts from this blog