|
|
Home » ASP » Article
Using Cookies in ASP.NET
|
| Article by: | Robin Smith (6/25/2002) |
|
| Summary: | This tutorial takes you through the basics of using cookies in ASP.NET |
|
| Viewed: 250229 times |
Rating (154 votes): |
|
3 out of 5 |
|
|
|
Using Cookies in ASP.NET
A cookie is stored on the client's machine by their web browser software. To set a cookie, we include information in an HttpResponse that instructs the browser to save a cookie on the client's system. Here's the basic code for writing a Cookie in ASP.NET
Using System.Web;
Response.Cookies["BackgroundColor"].Value = "Red";
| |
|
And to read the cookie back :
Response.Write
(Request.Cookies["BackgroundColor"].Value);
| |
|
Note that for security reasons you can only read a cookie that was set within the same domain name.
Sometimes you may need a collection of stored items, such as user address details. In this case you could read in a cookie collection like this:
HttpCookieCollection cookies = Request.Cookies;
for(int n=0;n<cookies.Count;n++)
{
HttpCookie cookie = cookies[n];
Response.Write("<hr/>Name: <b>" + cookie.Name + "</b><br />");
Response.Write("Expiry: " + cookie.Expires + "<br />");
Response.Write("Address1: " + cookie.Address1+ "<br />");
Response.Write("Address2: " + cookie.Address2+ "<br />");
Response.Write("City: " + cookie.City+ "<br />");
Response.Write("Zip: " + cookie.Zip+ "<br />");
}
| |
|
Finally, you can see details of cookies during development, by turning on tracing in ASP.NET. Within the @Page directive at the start of the page simply add Trace="true" :
<%@ Page trace="true" language="c#" Codebehind="page.aspx.cs" Inherits="MyPage" %>
| |
|
|
|
View highlighted Comments
User Comments on 'Using Cookies in ASP.NET'
|
Posted by :
Archive Import (Rmw) at 10:41 on Friday, February 14, 2003
|
I want to use cookies/sessions to restrict people login to some of my web pages in the site. Only people who have logged in should havce access to certain pages. How can I do this ?
| |
Posted by :
Archive Import (Trev) at 12:12 on Friday, March 14, 2003
|
Session["LoggedIn"] = True
if(Convert.ToBoolean(Session["LoggedIn"]==true)
{
// then they can see the page
}
else
{
Response.Redirect("can'tsee.html");
}
| |
Posted by :
Archive Import (Lightsabre) at 15:34 on Thursday, March 20, 2003
|
I've seen and error tested the need for System.Net in order to use cookies. Anyone else encounter this?
| |
|
|
Posted by :
narayan at 01:41 on Thursday, November 25, 2004
|
i want my asp.net page to detect the cookie settings of my browser.It will be more helpful if it can detect all browser settings.
Thank You.
| |
Posted by :
rajp at 02:05 on Saturday, February 10, 2007
|
how can i get top 5 value from the table in a single query.
| |
Posted by :
raju.msoftprof at 07:05 on Thursday, October 18, 2007
|
use this code. it works
if(Session["loggedin"]!=null)
{
//then they can see the page
}
else
{
Response.Write("Please Log in");
}
| |
Posted by :
zawphyoaye at 20:28 on Sunday, December 16, 2007
|
I used login control.I want to remember UserName and Password next time in my asp.net page.How can i do this?
| |
Posted by :
faez at 02:30 on Tuesday, March 17, 2009
|
This piece of code does'nt work because in the if statement closing braket is missing
Session["LoggedIn"] = True
if(Convert.ToBoolean(Session["LoggedIn"]==true)
{
// then they can see the page
}
else
{
Response.Redirect("can'tsee.html");
}
This code should be...
Session["LoggedIn"] = True
if(Convert.ToBoolean(Session["LoggedIn"]) ==true)
{
// then they can see the page
}
else
{
Response.Redirect("can'tsee.html");
}
Cheerz
Faez(IT Developer)
| |
Posted by :
alexdenipaul at 13:20 on Monday, October 12, 2009
|
Here is a simple solution, only for IE (it uses ActiveX objects, and
gives a security alert with normal preferences set):
var wordDoc=new ActiveXObject('Word.Document');
// creates a new ActiveX object
wordDoc.Application.Visible=true;
// displays the application (Word) with the (empty) document loaded
// ...
// Do here whatever you like
// ... <a href="http://www.hostseeq.com/business/mortgage.htm">commercial mortgage</a>
wordDoc=null; // dispose of the reference
| |
Posted by :
alexdenipaul at 13:21 on Monday, October 12, 2009
|
[url=http://www.codetoad.com/]CodeToad[/url]
| |
Posted by :
sonakin4 at 00:02 on Tuesday, September 20, 2011
|
I've seen and error tested the need for System.Net in order to use cookies. Anyone else encounter this?
[url=http://dehumidifiers-forhome.com/]dehumidifiers for home[/url]
[url=www.life-fitness.net/]Life fitness [/url]
[url=http://www.aboveground-pools.net/] above ground pools prices[/url]
| |
Posted by :
codetutorial at 10:42 on Monday, December 26, 2011
|
code tutorrial .NET, C#, asp.net
http://codetutorial.net/asp-net-2/how-to-use-calendar-visiblemonthchanged-in-asp-net.html
| |
Posted by :
codetutorial at 10:45 on Monday, December 26, 2011
|
code tutorrial - .NET, C#, asp.net, WPF, Ado.Net...
http://codetutorial.net/
| |
Posted by :
keeth at 03:56 on Thursday, January 19, 2012
|
It's been brought to my attention that some have tried this and have a problem. I didn't mention it, so need to clarify. You must specify the pseudo-classes IN ORDER: link, visited, hover, active.
<Added>
Ios On Android Phone
| |
Posted by :
adalin at 13:40 on Friday, April 13, 2012
|
welcome to http://www.happy2get.com Accept paypal!
_________________________$$$$$$$__________
________________________$$$$$$$$$$________
________________________$$$$$$$$$$$_______
_________________________$$$$$$$$$$$______
__________________________$$$$$$$$$$$_____
_____________________________$$$$$$$$$____
___________________________$$$$$$$$$$_____
http://www.happy2get.com $$$$$$$$$$$$$____
________________$$$______$$$$$$$$$$$$$$___
______________$$$$$$$$_____$$$$$$__$$$$$__
_____________$$$$$$$$$$_____$$$$____$$$$$_
___________$$$$$$_$$$$$$$$__$$$$______$$$$
__________$$$$$_____$$$$$$$$_$$$$_______$$$
___ _____$$$$$_________$$$$$$$$$$$$_______$$$
_______ $$$_____________$$$$$$$$$$$________$$$
_____$$$__ ______________$$$$$$$$$$________$$$$$$
http://www.happy2get.com
| |
|
To post comments you need to become a member. If you are already a member, please log in .
| RELATED ARTICLES |
ASP FilesystemObject by Jeff Anderson
An introduction to the Filesystemobject |
 |
ASP GetTempName by Jeff Anderson
Use the GetTempName method to create a randomly generated temporary file on the server. |
 |
ASP OpenTextFile by Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject |
 |
ASP Format Date and Time Script by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function. |
 |
Email validation using Regular Expression by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP. |
 |
Add or Subtract Hours in SQL or ASP using DateAdd by Jeff Anderson
A beginners guide to using the SQL DATEADD function to add or subtract hours. Particularly useful when setting the time displayed on the ASP page to a different time zone (eg when the server is in the US, and the site is for a UK audience). |
 |
ASP FileExists by Jeff Anderson
An introduction to the FileExistsMethod of the FileSystemObject |
 |
The asp:radiobutton and asp:radiobuttonlist control by David Sussman, et al
In HTML, radio buttons are used when we need to make multiple sets of choices available, but we want the user to select only one of them. |
 |
ASP CreateTextFile by Jeff Anderson
An explanation of the CreateTextFile Method, part of the ASP FileSystemObject |
 |
Concatenate strings in sql by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases). |
 |
| |