.netcore使用cookie

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Fast.NetCore.Web.Helper
{
  public static class CookieHelper
  {

    public static void SetCookies(this HttpContext httpContext,string key,string value,int minutes=30)
    {
      httpContext.Response.Cookies.Append(key, value, new CookieOptions
      {
        Expires = DateTime.Now.AddMinutes(minutes)
      });
    }
    public static void DeleteCookie(this HttpContext httpContext,string key)
    {
      httpContext.Response.Cookies.Delete(key);
    }
    public static string GetCookieValue(this HttpContext httpContext,string key)
    {
      httpContext.Request.Cookies.TryGetValue(key, out string value);
      return value;
    }





  }
}

 

posted @ 2021-10-22 16:49  亦承  阅读(327)  评论(0)    收藏  举报