using Common.Http;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
//using Liger.Common;
namespace LigerRM.Common
{
///
/// 不支持 cookie values
///
public class CookieHelper
{
#region 获取Cookie
///
/// 获得Cookie的值
///
///
///
public static string GetCookieValue(string cookieName)
{
HttpRequest request = MyHttpContext.Current.Request;
if (request != null)
return request.Cookies[cookieName];
return "";
}
///
/// 获得Cookie的值
///
///
///
//public static string GetCookieValue(HttpCookie cookie)
//{
// if (cookie != null)
// {
// return cookie.Value;
// }
// return "";
//}
///
/// 获得Cookie
///
///
///
public static string GetCookie(string cookieName)
{
HttpRequest request = MyHttpContext.Current.Request;
if (request != null)
return request.Cookies[cookieName];
return null;
}
#endregion
#region 删除Cookie
///
/// 删除Cookie
///
///
public static void RemoveCookie(string cookieName)
{
HttpResponse response = MyHttpContext.Current.Response;
if (response != null)
{
response.Cookies.Delete(cookieName);
}
}
#endregion
#region 设置/修改Cookie
///
/// 设置Cookie
///
///
///
///
///
public static void SetCookie(string cookieName,string value, DateTime? expires)
{
//Guard.IsNotNullOrEmpty(cookieName, "cookieName");
HttpResponse response = MyHttpContext.Current.Response;
if (response != null)
{
response.Cookies.Delete(cookieName);
response.Cookies.Append(cookieName, value, new CookieOptions
{
Expires = expires
});
}
}
#endregion
#region 添加Cookie
///
/// 添加为Cookie.Values集合
///
///
///
///
///
public static void AddCookie(string cookieName, string value, DateTime expires)
{
//Guard.IsNotNullOrEmpty(cookieName, "cookieName");
//HttpCookie cookie = new HttpCookie(cookieName);
//cookie.Expires = expires;
//cookie.Value = value;
//AddCookie(cookie);
HttpResponse response = MyHttpContext.Current.Response;
response.Cookies.Append(cookieName, value, new CookieOptions
{
Expires = expires
});
}
///
/// 添加Cookie
///
///
//public static void AddCookie(HttpCookie cookie)
//{
// HttpResponse response = MyHttpContext.Current.Response;
// if (response != null)
// {
// //指定客户端脚本是否可以访问[默认为false]
// cookie.HttpOnly = true;
// //指定统一的Path,比便能通存通取
// cookie.Path = "/";
// //设置跨域,这样在其它二级域名下就都可以访问到了
// //cookie.Domain = "nas.com";
// response.AppendCookie(cookie);
// }
//}
#endregion
}
}