YiShaAdmin icon indicating copy to clipboard operation
YiShaAdmin copied to clipboard

IHttpContextAccessor.HttpContext

Open veigarwang opened this issue 3 years ago • 1 comments

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也是这样

veigarwang avatar May 11 '21 06:05 veigarwang

IHttpContextAccessor.HttpContext Property Gets or sets the current HttpContext. Returns null if there is no active HttpContext. 官网上显示是no active时会返回null,不太理解,求教

veigarwang avatar May 11 '21 06:05 veigarwang