1、实例一: 
     .Net以其强大的功能而著称。所以在.Net中提供了许多针对Cookie的类,可以很方便地完成对Cookie的各类操作。在.Net中有一个类HttpCookie,这个类中保存了Cookie的全部信息,包括Cookie名称、Cookie值、有效期等。我们在使用时先新建一个HttpCookie的对象,然后用Response.Cookies下的方法来操作这个对象就可以了。
      声明一个HttpCookie对象的方法为:HttpCookie cookiename = new HttpCookie(Cookie的名称, Cookie的值);其中,cookiename是对象名。然后,我们就可以为cookiename的各个属性赋值。
      当向系统中添加该Cookie时只要用Response.AppendCookie(cookiename);就可以了,或者用Response.Cookies.Add(cookiename);。读取Cookie时用Response.Cookies.Get(Cookie名称);删除Cookie时用Response.Cookies.Remove(Cookie名称);,但我试了一下,删不掉。所以还是用以前的思路,把该Cookie的有效期设置为过时就可以了,方法如下:
    HttpCookie ckUserInfo = Request.Cookies[txtUserName.Text.Trim()];
    ckUserInfo.Expires = DateTime.Today.AddDays(-1);
    Response.Cookies.Set(ckUserInfo);
      以上操作就可以完成Cookie的基本操作。因为我还想实现当用户输完用户名后把焦点从用户名框转移到密码框时自动在密码框中输出该用户对应的密码,所以还得增加一个javascript处理函数showpassword()。这个函数完全和上面DHTML中的showpassword()一样。所以,在.Net中完全可以使用以前DHTML中的方法。我们只要在页面导入时为用户名框添加一个onblur属性即可。Login.aspx的代码如下:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>无标题页</title>
    </head>
    <script>
        function GetCookie (name)
        {
    var arg = name + "=";
    var alen = arg.length;
    var clen = window.document.cookie.length;
    var i = 0;
    while (i < clen)
    {
    var j = i + alen;
    if (window.document.cookie.substring(i, j) == arg) return getCookieVal (j);
    i = window.document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
    break;
    }
    return null;
        }
        function getCookieVal (offset)
        {
    var endstr = window.document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = window.document.cookie.length;
    return unescape(window.document.cookie.substring(offset, endstr));
        }
        function showpassword()
        {
    var p=GetCookie(document.all.txtUserName.value);
    if(p!=null)
    document.all.txtPassword.value= p;
        }
    </script>
    <body>
        <form id="form1" runat="server">
        <div>
            请输入用户名:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
            请输入密码: &nbsp; &nbsp;<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"
                Width="149px"></asp:TextBox>&nbsp;<asp:CheckBox ID="chkRem" runat="server" Text="记住用户名和密码" /><br />
            &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<asp:Button ID="btnRem" runat="server" OnClick="btnRem_Click"
                Text="记录" />
            <asp:Button ID="btnDel" runat="server" OnClick="btnDel_Click" Text="删除" />
    
        </div>
        </form>
    </body>
    </html>
    Login.aspx.cs的代码如下:
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtUserName.Attributes.Add("onblur", "showpassword()");
        }

        protected void  btnRem_Click(object sender, EventArgs e)
        {
            if (chkRem.Checked)
            {
                HttpCookie ckUserInfo = new HttpCookie(txtUserName.Text.Trim(), txtPassword.Text);
                ckUserInfo.Expires = DateTime.Now.AddYears(1);
                Response.AppendCookie(ckUserInfo);
            }
        }
        protected void btnDel_Click(object sender, EventArgs e)
        {
            HttpCookie ckUserInfo = Request.Cookies[txtUserName.Text.Trim()];
            ckUserInfo.Expires = DateTime.Today.AddDays(-1);
            Response.Cookies.Set(ckUserInfo);
        }
    }
      好了,大功告成。如果大家还想进行更高级的操作的话,可以用这些基本功能进行开发,思路都是一样的。有什么更好的思路可以提出来,我们共同交流。 


2、实例二:
HttpCookie UserCookie = new HttpCookie("userName"); 
 UserCookie["name"]=dr["user"].ToString();

UserCookie.Expires=DateTime.Now.AddDays(1);//这里设置要保存多长时间.
 Response.Cookies.Add(UserCookie);
 

读取的时候
HttpCookie cookie=Request.Cookies["userName"];
string name=cookie.Values["name"];

清除cookie

 

HttpCookie cookie=Request.Cookies["userName"];
   if(cookie!=null)
   {
    cookie.Expires=DateTime.Now.AddDays(-2);    
    Response.Cookies.Set(cookie);
   }

Posted on 2008-11-12 17:49  hesen  阅读(642)  评论(0编辑  收藏  举报