Using Cookies in asp+

Source: Internet
Author: User
Tags button type datetime set cookie tostring domain
<%@ Page language= "VB"%>

<script language= "VB" runat= "Server" >
Const cookie_name as String = "Test-cookie-name"
Const Cookie_value as String = "Test-cookie-value"

' Declare our Cookie Object
Dim Objcookieobject as HttpCookie

Sub Btnsetcookie_onclick (Sender as Object, E as EventArgs)
' Create a cookie object-i ' m passing name and value,
' But you can also pass in a name and set the value later.
' IE. Objcookieobject = New HttpCookie (cookie_name)
Objcookieobject = New HttpCookie (cookie_name, Cookie_value)

' We already set these above!
' Objcookieobject.name = Cookie_name
' Objcookieobject.value = Cookie_value

' Additional Cookie Properties:
Objcookieobject.expires = New DateTime (2001, 12, 31, 23, 59, 59)

' Normally can leave these alone.
' The defaults'll work fine for most uses.
' Objcookieobject.domain = ' www.domain.com '
' Objcookieobject.path = '/path/'
' Objcookieobject.secure = True

Response.appendcookie (Objcookieobject)
End Sub

Sub Btnremovecookie_onclick (Sender as Object, E as EventArgs)
Objcookieobject = New HttpCookie (cookie_name)

' Expire it on the ' I am ' born just so we ' re sure it ' s a date in the past.
Objcookieobject.expires = New DateTime (1974, 11, 12)

Response.appendcookie (Objcookieobject)
End Sub

Sub Btngetcookie_onclick (Sender as Object, E as EventArgs)
Objcookieobject = Request.Cookies (cookie_name)

If not (objcookieobject = null) Then
Lblcookiedetails.text = Objcookieobject.name

Lblcookiedetailsname.text = Objcookieobject.name
Lblcookiedetailsvalue.text = Objcookieobject.value
Lblcookiedetailsexpires.text = objCookieObject.Expires.ToString
Lblcookiedetailsdomain.text = Objcookieobject.domain
Lblcookiedetailspath.text = Objcookieobject.path
Lblcookiedetailssecure.text = objCookieObject.Secure.ToString
Lblcookiedetailshaskeys.text = objCookieObject.HasKeys.ToString
Else
Lblcookiedetails.text = "Cookie not set!"

Lblcookiedetailsname.text = ""
Lblcookiedetailsvalue.text = ""
Lblcookiedetailsexpires.text = ""
Lblcookiedetailsdomain.text = ""
Lblcookiedetailspath.text = ""
Lblcookiedetailssecure.text = ""
Lblcookiedetailshaskeys.text = ""
End If

' I ' m ignoring collections. They ' re outside the realm of this basic sample.
' Fyi:additional properties related to Cookie collections:values, Item
End Sub
</script>

<body>


<form action= "cookies.aspx" method= "POST" runat= "Server" >
<asp:button type= "Submit" id= "Btnsetcookie" text= "Set Cookie"
runat= "Server"/>
<asp:button type= "Submit" id= "Btnremovecookie" text= "Remove Cookie"
runat= "Server"/>

<p>
To the cookies ' s current status for you ' ll need to click below. This is because the response
Which adds or deletes the cookie happens after the ' request is ' already done. As such, those changes aren ' t
Available from the request collection until the next request.
</p>

<asp:button type= "Submit" id= "Btngetcookie" text= "Get Cookie Details"
runat= "Server"/>
</form>

<p>
<strong>details of:</strong> <asp:label id= "lblcookiedetails" runat= "Server"/>
</p>

<table border= "1" >
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td><asp:label id= "Lblcookiedetailsname" runat= "Server"/></td>
</tr>
<tr>
<td>Value</td>
<td><asp:label id= "Lblcookiedetailsvalue" runat= "Server"/></td>
</tr>
<tr>
<td>Expires</td>
<td><asp:label id= "Lblcookiedetailsexpires" runat= "Server"/></td>
</tr>
<tr>
<td>Domain</td>
<td><asp:label id= "Lblcookiedetailsdomain" runat= "Server"/></td>
</tr>
<tr>
<td>Path</td>
<td><asp:label id= "Lblcookiedetailspath" runat= "Server"/></td>
</tr>
<tr>
<td>Secure</td>
<td><asp:label id= "lblcookiedetailssecure" runat= "Server"/></td>
</tr>
<tr>
<td>has keys</td>
<td><asp:label id= "Lblcookiedetailshaskeys" runat= "Server"/></td>
</tr>
</tbody>
</table>

</body>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.