CookieHelper.cs 804 B

123456789101112131415161718192021222324252627
  1. namespace AipGateway.API.Utils
  2. {
  3. public static class CookieHelper
  4. {
  5. public static void SetCookie(this HttpContext context, string key, string value, double minutes)
  6. {
  7. context.Response.Cookies.Append(key, value, new CookieOptions
  8. {
  9. Expires = DateTime.Now.AddMinutes(minutes)
  10. });
  11. }
  12. public static void DeleteCookie(this HttpContext context, string key)
  13. {
  14. context.Response.Cookies.Delete(key);
  15. }
  16. public static string GetCookie(this HttpContext context, string key)
  17. {
  18. context.Request.Cookies.TryGetValue(key, out string value);
  19. if (string.IsNullOrEmpty(value))
  20. value = string.Empty;
  21. return value;
  22. }
  23. }
  24. }