在ASP+中使用Cookie

1@ Page Language="VB" 
 1<script language="VB" runat="server">   
 2Const COOKIE_NAME As String = "test-cookie-name"   
 3Const COOKIE_VALUE As String = "test-cookie-value"   
 4  
 5' Declare our cookie object   
 6Dim objCookieObject As HttpCookie   
 7  
 8Sub btnSetCookie_OnClick(Sender As Object, E As EventArgs)   
 9' Create a cookie object - I'm passing name and value,   
10' but you can also pass in a name and set the value later.   
11' ie. objCookieObject = New HttpCookie(COOKIE_NAME)   
12objCookieObject = New HttpCookie(COOKIE_NAME, COOKIE_VALUE)   
13  
14' We already set these above!   
15'objCookieObject.Name = COOKIE_NAME   
16'objCookieObject.Value = COOKIE_VALUE   
17  
18' Additional cookie properties:   
19objCookieObject.Expires = New DateTime(2001, 12, 31, 23, 59, 59)   
20  
21' Normally you can leave these alone.   
22' The defaults will work fine for most uses.   
23'objCookieObject.Domain = "www.domain.com"   
24'objCookieObject.Path = "/path/"   
25'objCookieObject.Secure = True   
26  
27Response.AppendCookie(objCookieObject)   
28End Sub   
29  
30Sub btnRemoveCookie_OnClick(Sender As Object, E As EventArgs)   
31objCookieObject = New HttpCookie(COOKIE_NAME)   
32  
33' Expire it on the day I was born just so we're sure it's a date in the past.   
34objCookieObject.Expires = New DateTime(1974, 11, 12)   
35  
36Response.AppendCookie(objCookieObject)   
37End Sub   
38  
39Sub btnGetCookie_OnClick(Sender As Object, E As EventArgs)   
40objCookieObject = Request.Cookies(COOKIE_NAME)   
41  
42If Not(objCookieObject = null) Then   
43lblCookieDetails.Text = objCookieObject.Name   
44  
45lblCookieDetailsName.Text = objCookieObject.Name   
46lblCookieDetailsValue.Text = objCookieObject.Value   
47lblCookieDetailsExpires.Text = objCookieObject.Expires.ToString   
48lblCookieDetailsDomain.Text = objCookieObject.Domain   
49lblCookieDetailsPath.Text = objCookieObject.Path   
50lblCookieDetailsSecure.Text = objCookieObject.Secure.ToString   
51lblCookieDetailsHasKeys.Text = objCookieObject.HasKeys.ToString   
52Else   
53lblCookieDetails.Text = "Cookie Not Set!"   
54  
55lblCookieDetailsName.Text = ""   
56lblCookieDetailsValue.Text = ""   
57lblCookieDetailsExpires.Text = ""   
58lblCookieDetailsDomain.Text = ""   
59lblCookieDetailsPath.Text = ""   
60lblCookieDetailsSecure.Text = ""   
61lblCookieDetailsHasKeys.Text = ""   
62End If   
63  
64' I'm ignoring collections. They're outside the realm of this basic sample.   
65' FYI: Additional properties related to cookie collections: Values, Item   
66End Sub   
67</script>
 1<html>
 2<body>
 3<h4>The cookie name we're using for this sample is: <em>```
 4= COOKIE_NAME 
 5```</em></h4>
 6<form action="cookies.aspx" method="post" runat="server">
 7<asp:button id="btnSetCookie" onclick="btnSetCookie_OnClick" runat="server" text="Set Cookie" type="submit"></asp:button>
 8<asp:button id="btnRemoveCookie" onclick="btnRemoveCookie_OnClick" runat="server" text="Remove Cookie" type="submit"></asp:button>
 9<p>   
10To see the cookie's current status you'll need to click below. This is because the response   
11which adds or deletes the cookie happens after the request is already done. As such, those changes aren't   
12available from the request collection until the next request.   
13</p>
14<asp:button id="btnGetCookie" onclick="btnGetCookie_OnClick" runat="server" text="Get Cookie Details" type="submit"></asp:button>
15</form>
16<p>
17<strong>Details of:</strong> <asp:label id="lblCookieDetails" runat="server"></asp:label>
18</p>
19<table border="1">
20<thead>
21<tr>
22<th>Property</th>
23<th>Value</th>
24</tr>
25</thead>
26<tbody>
27<tr>
28<td>Name</td>
29<td><asp:label id="lblCookieDetailsName" runat="server"></asp:label></td>
30</tr>
31<tr>
32<td>Value</td>
33<td><asp:label id="lblCookieDetailsValue" runat="server"></asp:label></td>
34</tr>
35<tr>
36<td>Expires</td>
37<td><asp:label id="lblCookieDetailsExpires" runat="server"></asp:label></td>
38</tr>
39<tr>
40<td>Domain</td>
41<td><asp:label id="lblCookieDetailsDomain" runat="server"></asp:label></td>
42</tr>
43<tr>
44<td>Path</td>
45<td><asp:label id="lblCookieDetailsPath" runat="server"></asp:label></td>
46</tr>
47<tr>
48<td>Secure</td>
49<td><asp:label id="lblCookieDetailsSecure" runat="server"></asp:label></td>
50</tr>
51<tr>
52<td>Has Keys</td>
53<td><asp:label id="lblCookieDetailsHasKeys" runat="server"></asp:label></td>
54</tr>
55</tbody>
56</table>
57</body>
58</html>
Published At
Categories with Web编程
comments powered by Disqus