1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using System;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.AspNetCore.Http;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- using YiSha.Util.Extension;
- namespace YiSha.Util
- {
- public class CookieHelper
- {
-
-
-
-
-
-
- public void WriteCookie(string sName, string sValue, bool httpOnly = true)
- {
- IHttpContextAccessor hca = GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>();
- CookieOptions option = new CookieOptions();
- option.Expires = DateTime.Now.AddDays(30);
- option.HttpOnly = httpOnly;
- hca?.HttpContext?.Response.Cookies.Append(sName, sValue, option);
- }
-
-
-
-
-
-
-
- public void WriteCookie(string sName, string sValue, int expires, bool httpOnly = true)
- {
- IHttpContextAccessor hca = GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>();
- CookieOptions option = new CookieOptions();
- option.Expires = DateTime.Now.AddMinutes(expires);
- option.HttpOnly = httpOnly;
- hca?.HttpContext?.Response.Cookies.Append(sName, sValue, option);
- }
-
-
-
-
-
- public string GetCookie(string sName)
- {
- IHttpContextAccessor hca = GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>();
- return hca?.HttpContext?.Request.Cookies[sName];
- }
-
-
-
-
- public void RemoveCookie(string sName)
- {
- IHttpContextAccessor hca = GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>();
- hca?.HttpContext?.Response.Cookies.Delete(sName);
- }
- }
- }
|