Set Cookie DateTime - CSharp System.Web

CSharp examples for System.Web:Cookie

Description

Set Cookie DateTime

Demo Code


using System.Web;
using System.Text;
using System.Net;
using System.Linq;
using System.Configuration;
using System.Collections.Generic;
using System;// w ww.  j a  v a  2s.com

public class Main{
        public static void SetCookie(string cookieName, string key, string value, DateTime? expires)
        {
            HttpResponse response = HttpContext.Current.Response;
            if (response != null)
            {
                HttpCookie cookie = response.Cookies[cookieName];
                if (cookie != null)
                {
                    if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                        cookie.Values.Set(key, value);
                    else
                        if (!string.IsNullOrEmpty(value))
                            cookie.Value = value;
                    if (expires != null)
                        cookie.Expires = expires.Value;
                    response.SetCookie(cookie);
                }
            }

        }
        public static void SetCookie(string cookieName, DateTime expires)
        {
            SetCookie(cookieName, null, null, expires);
        }
        public static void SetCookie(string key, string value, DateTime expires)
        {
            SetCookie(key, null, value, expires);
        }
        public static void SetCookie(string key, string value)
        {
            SetCookie(key, null, value, null);
        }
        public static void SetCookie(string cookieName, string key, string value)
        {
            SetCookie(cookieName, key, value, null);
        }
}

Related Tutorials