asp.net core 3.1 中使用cookie

 

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),

            });

 

posted on 2020-11-02 11:05  落叶子  阅读(957)  评论(0编辑  收藏  举报

导航