1.在ConfigureServices中添加
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
2.在Configure中添加
app.UseCookiePolicy();
3.代码中获取cookie
var cookie = HttpContext.Request.Cookies.FirstOrDefault(m => m.Key =="cookiename"); if (cookie.Key != null) { string value = cookie.Value; }
4.代码中保存cookie
Response.Cookies.Append("cookiename", "cookievalue", new CookieOptions { Expires = DateTime.Now.AddHours(5), });