.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
        
        Response.AppendCookie(objCookieObject)
    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)
        
        Response.AppendCookie(objCookieObject)
    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
        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>

<html>
<body>

<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" />

    <p>
    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.
    </p>

    <asp:Button type="submit" id="btnGetCookie" text="Get Cookie Details" OnClick="btnGetCookie_OnClick" 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>
</html>
----------------------------------------------------
come from Asp101.com

时间: 2024-09-08 05:00:08

.net beta2 操作cookie的例子(转)的相关文章

.net beta2 操作email的例子(转)

<%@ Page Language="VB" ClientTarget="downlevel" %><%@ Import Namespace="System.Web.Mail" %> <script language="VB" runat="server">    Sub btnSendMail_OnClick(Source As Object, E As EventAr

用JSP操作Cookie

cookie|js 说起来,Cookie应该是一种应用较久的技术了.早在HTML刚刚出现的时候,在每个独立的页面之间没有办法记录和标识不同的用户.后来人们就发明了Cookie技术,当用户访问网页时,它能够在访问者的机器上创立一个文件,我们把它叫作Cookie,写一段内容进去,来标识不同的用户.如果下次用户再访问这个网页的时候,它又能够读出这个文件里面的内容,这样网页就知道上次这个用户已经访问过该网页了. 虽然现在网页的制作技术比起几年以前已经发展了许多.不过有些时候,Cookie还是能够

JavaScript操作Cookie详解

 这篇文章主要介绍了JavaScript操作Cookie详解,本文讲解了什么是Cookie.Cookie基础知识.Cookie常见问题.cookie 有两种清除方式.Cookie基础用法.Cookie高级用法等内容,需要的朋友可以参考下     什么是 Cookie "cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie 的值." - w3school cooki

jQuery操作cookie方法实例教程_jquery

本文实例讲述了jQuery操作cookie的方法.分享给大家供大家参考.具体方法如下: 先来看看jq.cookie的aip 写入cookie 复制代码 代码如下: $.cookie("this-cookie","this-value",{     expires:10,//有效日期     path:"/",//cookie的路 径     domanin:    //cookie的域名     secure:true //true,cookie

JavaScript操作Cookie详解_基础知识

什么是 Cookie "cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScript 来创建和取回 cookie 的值." - w3school cookie 是访问过的网站创建的文件,用于存储浏览信息,例如个人资料信息. 从JavaScript的角度看,cookie 就是一些字符串信息.这些信息存放在客户端的计算机中,用于客户端计算机与服务器之间传递信息. 在JavaScript中可以通过 doc

基于 Jquery操作Cookie记录用户查询过信息

这是一个Cookie数据生成的列表, 每次单击查询会存储一个域名,并把最后一次查询的域名放在最上方.本例子最多存储10个,大家可以根据自己情况进行设置 下在咱们一起来看看是怎么实现的吧. 先写一个操作Cookie的JS文件如下 Code  代码如下 复制代码 function getid(id) {return (typeof id == 'string') ? document.getElementById(id) : id};function getOffsetTop(el, p) {var

JavaScript 操作 Cookie

原文http://www.cnblogs.com/Darren_code/archive/2011/11/24/Cookie.html  从事web开发也有些日子了,cookie 是个啥差不多能说明白,可是实际自己一上手操作就是得去搜索(你们懂的),结果被鄙视了...所以就写一篇博文做为自己的学习笔记,嘿嘿,博客的好处在此体现出来了.   什么是 Cookie "cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 Jav

javascript中应用 cookie 的 例子

cookie|javascript java中的cookie使用时非常的广泛的,尤其一些 在线 影音 播放的网站, 一般都是用服务器段脚本将 影音文件的名字 放到cookie中,然后在客户段 用 javascript 读取 .这样可以隐蔽 文件的名字,防止下载. 下面就把一个javascript中使用 cookie的例子发出来.大家共同学习.  写 cookie          var expiresTime=new Date();         expiresTime.setTime(ex

在ASP.NET中操作文件的例子

asp.net  在ASP.NET中操作文件的例子 1.写文件writefile.aspx <%@ Import Namespace="System.IO" %> '引入所需的NameSpace<%Response.write("Writing the content into Text File in ASP.NET <BR>")Dim strwriterobj As StreamWriter '声明一个StreamWriter对象s