using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace AUTS.Services.Extensions
{
///
/// cookie扩展
///
public static class CookieExtensions
{
///
/// 写cookie值
///
/// 名称
/// 值
public static void WriteCookie(string strName, string strValue)
{
HttpCookie httpCookie = HttpContext.Current.Request.Cookies[strName];
if (httpCookie == null)
{
httpCookie = new HttpCookie(strName);
}
httpCookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(httpCookie);
}
///
/// 写cookie值
///
/// 名称
/// 值
/// 过期时间(分钟)
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie httpCookie = HttpContext.Current.Request.Cookies[strName];
if (httpCookie == null)
{
httpCookie = new HttpCookie(strName);
}
httpCookie.Value = strValue;
httpCookie.Expires = DateTime.Now.AddMinutes((double)expires);
HttpContext.Current.Response.AppendCookie(httpCookie);
}
///
/// 读cookie值
///
/// 名称
/// cookie值
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
}
return "";
}
///
/// 检查Cookie,如果存在则为true
///
///
///
public static bool CheckCookie(string strName)
{
return HttpContext.Current.Request.Cookies[strName] != null;
}
///
/// 删除Cookie
///
///
public static void RemoveCookie(string strName)
{
HttpContext.Current.Request.Cookies.Remove(strName);
}
}
}