Use cookies to keep client information in ASP.NET

xiaoxiao2021-03-06  115

Author: Unknown Author speed Please contact me eat my current stuff are fixed for food, so was not surprised, as the theme of this week's cookies. Cookies is used to store specific user information, which provides a useful way in the web program. For many years, JavaScript developers have conducted a lot of work on cookies. Similarly, ASP.NET also provides cookie access via System.Web space name. Although you should not use cookies to store some sensitive data, they are an excellent choice for processing lock data, such as color parameter selection or last accessed date.

Passing cookiescookie is a small file stored on the client computer. If you are a Windows user, you can view the cookies path in the user path, which is the Documents and Settings path. This path contains text files for this file name:

UserName @ Web Site Domain That Created the cookie

(User Name @ Establishing a Site Domain Name of Cookie)

The .NET System.Web space name contains three classes, you can use them to handle the client's cookies:

Httpcookie: Provides a way to build and operate the secure type of HTTP cookies.

HTTPRESPONSE: The cookies property allows client cookies to be operated.

HttpRequest: The cookies property allows access to the client's cookies.

The cookies properties of HttpResponse and HttpRequest objects will return an HTTPCOOKIECOLLECTION object that contains separate cookies to a collection, and get a separate cookies from the Collection.

The HTTPCookie class HTTPCookie class is established separate cookies for customer storage. Once the HTTPCookie object is established, you can add it to the cookies properties of the HttpResponse object. Similarly, you can access existing cookies via HttpRequest object. The HTTPCookie class contains the following public properties:

Domain: Gets or sets the domain name related to cookie, which can be used to limit cookie access to a specific area.

Expires: Get or sets the termination date and time of the cookie, you can set it to a past dates to automatically terminate or delete cookies.

Names: Get or set the cookie name.

PATH: Gets or sets the virtual path of the cookie. This property allows you to limit the cookie range, that is, access cookies can only be limited to a specific folder or path. Setting this property is limited to only access to all files in the specific path and the path.

Secure: Send a cookie value to indicate whether you use Secure Sockets Layer (SSL).

Value: Get or set a separate cookie value.

VALUES: Returns a collection of Key / Value contained in cookies.

Although these are not a most detailed list, it provides things needed to process cookies. For these properties, the following VB.NET sample gives the best understanding:

DIM TestCookie As New Httpcookie ("Lastvisited")

TestCookie.Value = datetime.now.tostring

TestCookie.expires = datetime.now.adddays (7) TestCookie.domain = "builder.com"

Response.cookies.add (TestCookie)

This code segment has established a new cookie named LastVisited and gives the current date and time value. Similarly, the cookie termination period is set to a week, the associated range is Populated. Once an object is established, the object can be added to the client's cookies collection through the ADD method of the Response.Cookies object. There are two methods in the HTTPCookie constructor:

Httpcookie Objectname = New httpcookie ("cookiename")

Httpcookie objectname = new httpcookie ("cookiename", "cookievalue")

Similarly, the Response object contains a setCookie method that can accept an HTTPCOOKIE object.

Where is my cookie? Once cookies are saved on the client, there are a variety of different ways to provide you access them. If you know the cookie name, you can easily access its value using the HttpResponse object. The following VB.NET rows displays the values ​​related to cookie:

Response.write (Request.Cookies ("LastvisiTed"). Value)

In addition, you can access a complete list of cookies through an HTTPCookIECollection object. This allows the cookie list to be accessed with a For loop. The following C # code illustrates this example:

HTTPCOOKIECOLLECTION COOKIES

HTTPCOOKIE ONECOOKIE;

Cookies = Request.cookies;

String [] cookieArray = cookies.allkeys;

For (int I = 0; i

OneCookie = Cookies [cookieArray [i]];

Response.write (OneCookie.Name "-" OneCookie.Value);

}

The corresponding code in VB.NET is as follows:

DIM I as integer

Dim OneCookie as httpcookie

For i = 0 to request.cookies.count - 1

OneCookie = Request.Cookies (i)

Response.write (OneCookie.name "-" OneCookie.Value)

Next i

Stability is also a point of view cookie files stored in the client machine, so your users can delete or change anything. In addition, users can also invalidate cookies. Based on this reason, remember not to rely on cookie data. You should save important information in servers - especially in a database.

Storage key information in a cookie is considered a low-level programming because this information is easily leaked because this information is located in a file of the client machine. In this regard, a method is to use SSL, which is a better way to avoid sensitive information.

Can I use cookies? Users can invalidate cookie support on their own browser. You can access these settings in your own code to determine if you support cookies. The REQUEST object satisfies this idea, the following VB.NET code shows this process: if Request.Browser.cookies = Trow Then

'' Use cookies

Else

'' No cookie support

END IF

You can use the cookie value in a federated code. The following C # code segment is tested on the cookie support and the result is displayed in a text box accordingly:

IF (Request.Browser.cookies == True)

{

IF (Request.Cookies ["Lastvisited1"] == NULL)

{

HTTPCOOKIE NewCookie = New Httpcookie ("Lastvisited1", DateTime.now.toString ());

Newcookie.expires = datetime.now.addyears (1);

Response.cookies.add (newcookie);

THIS.TXTNAME.TEXT = "IS this your first time?";

} else {

this.txtname.text = "We Haven't Seen You Since"

Request.cookies ["Lastvisited1"]. Value;

}

You can add this code segment to the Page_Load event in the ASP.NET page.

Another way of saving data ASP.NET provides a variety of ways to save specific user data. One of the old methods is cookies. For sensitive data, although cookies is not the best way, it is the best choice such as color parameter selection, the last access date, and other affinity options (Benign Items). Although these sensitive data is important, the data is lost when the user's computer crashes, which is not the end of the world.

转载请注明原文地址:https://www.9cbs.com/read-101657.html

New Post(0)