.net beta2 操作cookie的例子(轉)

cookie <%@ 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, 11, 12)

        ' Normally you can leave these alone.
        ' The defaults will work fine for most uses.
        'objCookieObject.Domain  = "www.domain.com"
        'objCookieObject.Path    = "/path/"
        'objCookieObject.Secure  = True
    End Sub

    Sub btnRemoveCookie_OnClick(Sender As Object, E As EventArgs)
        objCookieObject = New HttpCookie(COOKIE_NAME)
        ' Expire it on the day I was born just so we're sure it's a date in the past.
        objCookieObject.Expires = New DateTime(1974, 11, 12)
    End Sub

    Sub btnGetCookie_OnClick(Sender As Object, E As EventArgs)
        objCookieObject = Request.Cookies(COOKIE_NAME)

        ' In Beta 1 this worked fine... not with Beta 2.
        'If Not(objCookieObject = Nothing) Then
        If Not(objCookieObject Is Nothing) Then
            lblCookieDetails.Text        = objCookieObject.Name

            lblCookieDetailsName.Text    = objCookieObject.Name
            lblCookieDetailsValue.Text   = objCookieObject.Value
            ' For some reason I'm having trouble with this now...
            ' The setting seems to work, but I can't read it back.
            ' Anyone have any ideas? Email me -> john@asp101.com
            lblCookieDetailsExpires.Text = objCookieObject.Expires.ToString
            lblCookieDetailsDomain.Text  = objCookieObject.Domain
            lblCookieDetailsPath.Text    = objCookieObject.Path
            lblCookieDetailsSecure.Text  = objCookieObject.Secure.ToString
            lblCookieDetailsHasKeys.Text = objCookieObject.HasKeys.ToString
            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


<h4>The cookie name we're using for this sample is: <em><%= COOKIE_NAME %></em></h4>

<form action="cookie.aspx" method="post" runat="server">
    <asp:Button type="submit" id="btnSetCookie" text="Set Cookie" OnClick="btnSetCookie_OnClick" runat="server" />
    <asp:Button type="submit" id="btnRemoveCookie" text="Remove Cookie" OnClick="btnRemoveCookie_OnClick" runat="server" />

    To see the cookie's current status 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.

    <asp:Button type="submit" id="btnGetCookie" text="Get Cookie Details" OnClick="btnGetCookie_OnClick" runat="server" />

<strong>Details of:</strong> <asp:label id="lblCookieDetails" runat="server" />

<table border="1">
            <td><asp:label id="lblCookieDetailsName" runat="server" /></td>
            <td><asp:label id="lblCookieDetailsValue" runat="server" /></td>
            <td><asp:label id="lblCookieDetailsExpires" runat="server" /></td>
            <td><asp:label id="lblCookieDetailsDomain" runat="server" /></td>
            <td><asp:label id="lblCookieDetailsPath" runat="server" /></td>
            <td><asp:label id="lblCookieDetailsSecure" runat="server" /></td>
            <td>Has Keys</td>
            <td><asp:label id="lblCookieDetailsHasKeys" runat="server" /></td>

come from Asp101.com


E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >



如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。