Asp.Net cookies 通用类

 using System;
using System.Data;
using System.Configuration;
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;

///


/// cookies通用类
///

public class myCookie
{
 public myCookie()
 {
 }
    ///
    /// 设置cookies
    ///

    /// 主键
    ///
    /// 天数
    ///
    public bool SetCookies(string mainName, string mainValue, int days)
    {
        try
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[mainName];
            if (cookie == null)
            {
                cookie = new HttpCookie(mainName, mainValue);              
            }
            else
            {
                cookie.Value = mainValue;             
            }
            cookie.Expires = DateTime.Now.AddDays(days);
            System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }
    ///
    ///  获取cookies
    ///

    /// 主键
    ///
    public string GetCookies(string mainName)
    {
        HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[mainName];
        if (cookie != null)
        {
            return cookie.Value;
        }
        else
        {
            return null;
        }
    }
    ///
    /// 删除Cookies
    ///

    ///
    public bool DelCookies(string mainName)
    {
        try
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[mainName];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-1);
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
            return true;
        }
        catch
        {
            return false;
        }
    }
}

你可能感兴趣的:(cookies,asp.net,string,null,system,class)