YiShaAdmin
YiShaAdmin copied to clipboard
IHttpContextAccessor.HttpContext
public void WriteCookie(string sName, string sValue, bool httpOnly = true)
{
IHttpContextAccessor hca = GlobalContext.ServiceProvider?.GetService<IHttpContextAccessor>();
CookieOptions option = new CookieOptions { Expires = DateTime.Now.AddDays(30), HttpOnly = httpOnly };
hca?.HttpContext?.Response.Cookies.Append(sName, sValue, option);
}
部分情况下HttpContext会返回空导致无法写入Cookie,Session也是这样
IHttpContextAccessor.HttpContext Property Gets or sets the current HttpContext. Returns null if there is no active HttpContext. 官网上显示是no active时会返回null,不太理解,求教