2020-04-01 20:00:27 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Web;
|
2020-04-01 20:00:27 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Web.Common.AspNetCore
|
2020-04-01 20:00:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
public class AspNetCoreCookieManager : ICookieManager
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
public AspNetCoreCookieManager(IHttpContextAccessor httpContextAccessor)
|
|
|
|
|
|
{
|
|
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ExpireCookie(string cookieName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var httpContext = _httpContextAccessor.HttpContext;
|
|
|
|
|
|
|
|
|
|
|
|
if (httpContext is null) return;
|
|
|
|
|
|
|
|
|
|
|
|
var cookieValue = httpContext.Request.Cookies[cookieName];
|
|
|
|
|
|
|
2020-04-20 12:20:47 +02:00
|
|
|
|
httpContext.Response.Cookies.Append(cookieName, cookieValue ?? string.Empty, new CookieOptions()
|
2020-04-01 20:00:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
Expires = DateTime.Now.AddYears(-1)
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-29 13:44:21 +02:00
|
|
|
|
public string? GetCookieValue(string cookieName)
|
2020-04-01 20:00:27 +02:00
|
|
|
|
{
|
|
|
|
|
|
return _httpContextAccessor.HttpContext?.Request.Cookies[cookieName];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetCookieValue(string cookieName, string value)
|
|
|
|
|
|
{
|
2020-06-03 17:17:30 +02:00
|
|
|
|
_httpContextAccessor.HttpContext?.Response.Cookies.Append(cookieName, value, new CookieOptions()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
});
|
2020-04-01 20:00:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool HasCookie(string cookieName)
|
|
|
|
|
|
{
|
|
|
|
|
|
return !(GetCookieValue(cookieName) is null);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|