Click here to Skip to main content
15,861,168 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi please could you tell me any one to my above question
Posted
Comments
[no name] 10-Jul-12 22:32pm    
Do you have a specific question about some code that you have written? Or do you just want someone to google about using cookies for you?

I think this comprehensive guide on cookies is worth reading. this is very explanatory and will really help you.

Beginner's Guide to ASP.NET Cookies[^]
 
Share this answer
 
v2
Comments
SoMad 10-Jul-12 22:48pm    
I agree, +5.

Soren Madsen
Sergey Alexandrovich Kryukov 10-Jul-12 23:28pm    
Well, I agree, to, my 5.
--SA
Manas Bhardwaj 11-Jul-12 5:57am    
Good +5!
A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser.

The cookie contains information the Web application can read whenever the user visits the site.

Most browsers support cookies of up to 4096 bytes.

You can add cookies to the Cookies collection in a number of ways.

The following example shows two methods to write cookies:
C#
Response.Cookies["userName"].Value = "patrick";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("lastVisit");
aCookie.Value = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);


You can also store multiple name-value pairs in a single cookie. Which are referred to as subkeys.
For example, instead of creating two separate cookies named userName and lastVisit, you can create a single cookie named userInfo that has the subkeys userName and lastVisit.

To create a cookie with subkeys, you can use a variation of the syntax for writing a single cookie.
The following example shows two ways to write the same cookie, each with two subkeys:
C#
Response.Cookies["userInfo"]["userName"] = "patrick";
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);

HttpCookie aCookie = new HttpCookie("userInfo");
aCookie.Values["userName"] = "patrick";
aCookie.Values["lastVisit"] = DateTime.Now.ToString();
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);


By default, all cookies for a site are stored together on the client, and all cookies are sent to the server with any request to that site. In other words, every page in a site gets all of the cookies for that site. However, you can set the scope of cookies in two ways:

-Limit the scope of cookies to a folder on the server, which allows you to limit cookies to an application on the site.
To limit cookies to a folder on the server, set the cookie's Path property, as in the following example:
C#
HttpCookie appCookie = new HttpCookie("AppCookie");
appCookie.Value = "written " + DateTime.Now.ToString();
appCookie.Expires = DateTime.Now.AddDays(1);
appCookie.Path = "/Application1";
Response.Cookies.Add(appCookie);


-Set scope to a domain, which allows you to specify which subdomains in a domain can access a cookie.
To limit cookies scope to a domain, set the cookie's Domain property, as in this example:
C#
Response.Cookies["domain"].Value = DateTime.Now.ToString();
Response.Cookies["domain"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["domain"].Domain = "support.abc.com";

When the domain is set in this way, the cookie will be available only to pages in the specified subdomain.

You can also use the Domain property to create a cookie that can be shared among multiple subdomains, as shown in the following example:
C#
Response.Cookies["domain"].Value = DateTime.Now.ToString();
Response.Cookies["domain"].Expires = DateTime.Now.AddDays(1);
Response.Cookies["domain"].Domain = "abc.com";


Reading Cookies
When a browser makes a request to the server, it sends the cookies for that server along with the request.
In ASP.NET applications, cookies can be read using the HttpRequest object, which is available as the Request property of Page class.

The structure of the HttpRequest object is essentially the same as that of the HttpResponse object, so cookies can be read out of the HttpRequest object much the same way you wrote cookies into the HttpResponse object.

The following code example shows two ways to get the value of a cookie named username and display its value in a Label control:
C#
if(Request.Cookies["userName"] != null)
    Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);

if(Request.Cookies["userName"] != null)
{
    HttpCookie aCookie = Request.Cookies["userName"];
    Label1.Text = Server.HtmlEncode(aCookie.Value);
}


The following code example shows one way to get the value of a subkey:
C#
if(Request.Cookies["userInfo"] != null)
{
    Label1.Text = 
        Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);

    Label2.Text =
        Server.HtmlEncode(Request.Cookies["userInfo"]["lastVisit"]);
}


Refer: http://msdn.microsoft.com/en-us/library/aa289495(v=vs.71).aspx[^] for more.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 11-Jul-12 5:58am    
Nicely explained. Good +5!
Prasad_Kulkarni 11-Jul-12 6:10am    
Thank you Manas!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900