程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C# (Cookie)基本操作,

C# (Cookie)基本操作,

編輯:關於.NET

C# (Cookie)基本操作,


在Common中新建一個CookieHelper,全局調用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Common {
    public class CookieHelper {
        /// <summary>
        /// 設置cookie
        /// </summary>
        /// <param name="cookieName">cookie名稱</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="domain">作用域,為空就不寫入作用域</param>
        public static void SetCookie(String cookieName, String cookieValue, string domain) {
            if (String.IsNullOrEmpty(cookieName) || String.IsNullOrEmpty(cookieValue)) return;
            if (HttpContext.Current != null) {
                HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
                if (domain.Length > 0) {
                    cookie.Domain = domain;
                }
                cookie.HttpOnly = true;
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>
        /// 設置cookie
        /// </summary>
        /// <param name="cookieName">cookie名稱</param>
        /// <param name="cookieValue">cookie值</param>
        /// <param name="domain">作用域,為空就不寫入作用域</param>
        /// <param name="day">有效時間</param>
        public static void SetCookie(String cookieName, String cookieValue, string domain, int day) {
            if (String.IsNullOrEmpty(cookieName) || String.IsNullOrEmpty(cookieValue)) return;
            if (HttpContext.Current != null) {
                HttpCookie cookie = new HttpCookie(cookieName, cookieValue);
                if (domain.Length > 0) {
                    cookie.Domain = domain;
                }
                cookie.HttpOnly = true;
                cookie.Expires = DateTime.Now.AddDays(day);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>
        /// 設置cookie過期
        /// </summary>
        /// <param name="cookieName">需要過期的cookie名稱</param>
        public static void ExpireCookie(String cookieName) {
            if (String.IsNullOrEmpty(cookieName)) return;
            if (HttpContext.Current != null) {
                HttpCookie cookie = new HttpCookie(cookieName, string.Empty);
                cookie.HttpOnly = true;
                cookie.Expires = DateTime.Now.AddYears(-5);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>
        /// 獲取對應Cookie名稱的值
        /// </summary>
        /// <param name="cookieName">Cookie 的名稱</param>
        /// <returns></returns>
        public static string GetCookie(string cookieName) {
            if (string.IsNullOrEmpty(cookieName)) return string.Empty;
            if (System.Web.HttpContext.Current == null) return string.Empty;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return string.Empty;
            else return System.Web.HttpContext.Current.Request.Cookies[cookieName].Value;
        }
        /// <summary>
        /// 判斷對應的Cookie是否存在
        /// </summary>
        /// <param name="cookieName">Cookie 的名稱</param>
        /// <returns></returns>
        public static bool ExistCookie(string cookieName) {

            if (string.IsNullOrEmpty(cookieName) || System.Web.HttpContext.Current == null) return false;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null) return false;
            if (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value == null) return false;
            return (System.Web.HttpContext.Current.Request.Cookies[cookieName].Value.Length > 0);
        }
    }
}

  

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved