83 lines
2.6 KiB
C#
83 lines
2.6 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using System.Web;
|
|||
|
|
|
|||
|
|
namespace AUTS.Services.Extensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// cookie扩展
|
|||
|
|
/// </summary>
|
|||
|
|
public static class CookieExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 写cookie值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="strName">名称</param>
|
|||
|
|
/// <param name="strValue">值</param>
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 写cookie值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="strName">名称</param>
|
|||
|
|
/// <param name="strValue">值</param>
|
|||
|
|
/// <param name="strValue">过期时间(分钟)</param>
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 读cookie值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="strName">名称</param>
|
|||
|
|
/// <returns>cookie值</returns>
|
|||
|
|
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 "";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 检查Cookie,如果存在则为true
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="strName"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static bool CheckCookie(string strName)
|
|||
|
|
{
|
|||
|
|
return HttpContext.Current.Request.Cookies[strName] != null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 删除Cookie
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="strName"></param>
|
|||
|
|
public static void RemoveCookie(string strName)
|
|||
|
|
{
|
|||
|
|
HttpContext.Current.Request.Cookies.Remove(strName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|