Get Cookie Value - CSharp System.Web

CSharp examples for System.Web:Cookie

Description

Get Cookie Value

Demo Code


using System.Web;
using System.Text;
using System.Net;
using System.Linq;
using System.Configuration;
using System.Collections.Generic;
using System;//from ww w.ja  va  2s .  co  m

public class Main{

        public static string GetCookieValue(HttpCookie cookie, string key)
        {
            if (cookie != null)
            {
                if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
                    return cookie.Values[key];
                else
                    return cookie.Value;
            }
            return "";
        }
        public static string GetCookieValue(string cookieName, string key)
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request != null)
                return GetCookieValue(request.Cookies[cookieName], key);
            return "";
        }
        public static string GetCookieValue(string cookieName)
        {
            return GetCookieValue(cookieName, null);
        }
}

Related Tutorials