Core Session
net Session
Net Entity Framework Cookie
Net Core
CookieOptions option = new CookieOptions();
option.Expires = DateTime.Now.AddMilliseconds(10);
Response.Cookies.Append(key, value, option);
IHttpContextAccessor HttpContextAccessor
CORE COOKIE 进行封装
namespace WebAPI004.Coms
{public interface ICookieHelper{void SetCookie(string key, string value);string GetCookie(string key);void SetCookie(string key, string value, CookieOptions options);void DeleleCookie(string key);}
}
namespace WebAPI004.Coms
{public class CookieHelper : ICookieHelper{public IHttpContextAccessor HttpContextAccessor { get; }/// <summary>/// 依赖注入/// </summary>/// <param name="_httpContextAccessor"></param>public CookieHelper(IHttpContextAccessor _httpContextAccessor){HttpContextAccessor = _httpContextAccessor;} /// <summary>/// 删除Cookie/// </summary>/// <param name="key">Cookie的key</param>/// <returns></returns>public void DeleleCookie(string key){this.HttpContextAccessor.HttpContext.Response.Cookies.Delete(key);}public string GetCookie(string key){return this.HttpContextAccessor?.HttpContext.Request.Cookies[key];}public void SetCookie(string key, string value){this.HttpContextAccessor.HttpContext.Response.Cookies.Append(key, value);}public void SetCookie(string key, string value, CookieOptions options){this.HttpContextAccessor.HttpContext.Response.Cookies.Append(key, value,options);}}
}
》》》注册 服务
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();builder.Services.AddSingleton<ICookieHelper, CookieHelper>();