54 lines
1.3 KiB
C#
54 lines
1.3 KiB
C#
namespace Kit.Helpers
|
|
{
|
|
using System;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
public class CookieHelper
|
|
{
|
|
public static string GetValue(HttpContext context, string key)
|
|
{
|
|
String result = String.Empty;
|
|
if (context != null && context.Request.Cookies.ContainsKey(key))
|
|
{
|
|
return context.Request.Cookies[key];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static void SetValue(HttpContext context, string key, string value, DateTime expires)
|
|
{
|
|
if (context == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (context.Request.Cookies.ContainsKey(key))
|
|
{
|
|
context.Response.Cookies.Delete(key);
|
|
}
|
|
|
|
context.Response.Cookies.Append(key, value, new CookieOptions
|
|
{
|
|
Expires = expires,
|
|
|
|
});
|
|
}
|
|
|
|
public static void SetValue(HttpContext context, string key, string value)
|
|
{
|
|
SetValue(context, key, value, DateTime.MaxValue);
|
|
}
|
|
|
|
public static void RemoveCookie(HttpContext context, string key)
|
|
{
|
|
if (context == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
context.Response.Cookies.Delete(key);
|
|
}
|
|
|
|
}
|
|
}
|