ASP.NET.in.a.Nutshell.Second.Edition [Electronic resources]

G. andrew Duthie; matthew Macdonald

نسخه متنی -صفحه : 873/ 154
نمايش فراداده

HttpCookie

System.Web (system.web.dll)sealed class

Use this class to create a client-side cookie. The HttpCookie constructor takes a string representing the name of the cookie. After creating a cookie, you can add information to it in the form of name/value pairs by using the HttpCookieCollection.Add( ) method as follows: objCookies.Values.Add("Name", "John");. Values can be retrieved by using their name with a syntax like strName = objCookies.Values["Name"];.

To send the cookie to the client browser as part of the HTTP response, use the AppendCookie( ) method of the HttpResponse class. This method stores the cookie on the client browser. You can then retrieve a cookie from the HttpResponse class's cookie collection on other pages by using the cookie name, as in Response.Cookies["NameList"]. To ensure compatibility with all browsers, you should not store more than 4096 bytes in a single cookie.

To make a cookie persist between sessions, set the Expires property for the HttpCookie to a date in the future. You can also set the Secure property to True to restrict the cookie to Secure Socket Layer (SSL) transmission. A cookie is much less secure than Application or Session state variables, as information is maintained on the client and transmitted back and forth continuously.

public sealed class 

HttpCookie { // Public Constructors public

HttpCookie (string

name ); public

HttpCookie (string

name , string

value ); // Public Instance Properties public string

Domain {set; get; } public DateTime

Expires {set; get; } public bool

HasKeys {get; } public string

Name {set; get; } public string

Path {set; get; } public bool

Secure {set; get; } public string

this [string

key ]{set; get; } public string

Value {set; get; } public NameValueCollection

Values {get; } }

Returned By

HttpCookieCollection.{Get( ), this}, System.Web.Security.FormsAuthentication.GetAuthCookie( )

Passed To

HttpCookieCollection.{Add( ), Set( )}, HttpResponse.{AppendCookie( ), SetCookie( )}