2020-04-01 20:00:27 +02:00
|
|
|
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
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
namespace Umbraco.Cms.Web.Common.AspNetCore;
|
2020-04-01 20:00:27 +02:00
|
|
|
|
2025-03-21 15:20:08 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// An <see cref="ICookieManager"/> implementation for ASP.NET Core.
|
|
|
|
|
/// </summary>
|
2022-05-09 09:39:46 +02:00
|
|
|
public class AspNetCoreCookieManager : ICookieManager
|
|
|
|
|
{
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
2020-04-01 20:00:27 +02:00
|
|
|
|
2025-03-21 15:20:08 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="AspNetCoreCookieManager"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="httpContextAccessor">The <see href="IHttpContextAccessor" />.</param>
|
2022-05-09 09:39:46 +02:00
|
|
|
public AspNetCoreCookieManager(IHttpContextAccessor httpContextAccessor) =>
|
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2020-04-01 20:00:27 +02:00
|
|
|
|
2025-03-21 15:20:08 +01:00
|
|
|
/// <inheritdoc/>
|
2022-05-09 09:39:46 +02:00
|
|
|
public void ExpireCookie(string cookieName)
|
|
|
|
|
{
|
|
|
|
|
HttpContext? httpContext = _httpContextAccessor.HttpContext;
|
2020-04-01 20:00:27 +02:00
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
if (httpContext is null)
|
2020-04-01 20:00:27 +02:00
|
|
|
{
|
2022-05-09 09:39:46 +02:00
|
|
|
return;
|
2020-04-01 20:00:27 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 09:39:46 +02:00
|
|
|
var cookieValue = httpContext.Request.Cookies[cookieName];
|
2020-06-03 17:17:30 +02:00
|
|
|
|
2025-03-21 15:20:08 +01:00
|
|
|
httpContext.Response.Cookies.Append(
|
|
|
|
|
cookieName,
|
|
|
|
|
cookieValue ?? string.Empty,
|
|
|
|
|
new CookieOptions
|
|
|
|
|
{
|
|
|
|
|
Expires = DateTime.Now.AddYears(-1),
|
|
|
|
|
});
|
2022-05-09 09:39:46 +02:00
|
|
|
}
|
2020-04-01 20:00:27 +02:00
|
|
|
|
2025-03-21 15:20:08 +01:00
|
|
|
/// <inheritdoc/>
|
2022-05-09 09:39:46 +02:00
|
|
|
public string? GetCookieValue(string cookieName) => _httpContextAccessor.HttpContext?.Request.Cookies[cookieName];
|
2020-04-01 20:00:27 +02:00
|
|
|
|
2025-03-21 15:20:08 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public void SetCookieValue(string cookieName, string value, bool httpOnly, bool secure, string sameSiteMode)
|
|
|
|
|
{
|
|
|
|
|
SameSiteMode sameSiteModeValue = ParseSameSiteMode(sameSiteMode);
|
|
|
|
|
_httpContextAccessor.HttpContext?.Response.Cookies.Append(
|
|
|
|
|
cookieName,
|
|
|
|
|
value,
|
|
|
|
|
new CookieOptions
|
|
|
|
|
{
|
|
|
|
|
HttpOnly = httpOnly,
|
|
|
|
|
SameSite = sameSiteModeValue,
|
|
|
|
|
Secure = secure,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static SameSiteMode ParseSameSiteMode(string sameSiteMode) =>
|
|
|
|
|
Enum.TryParse(sameSiteMode, ignoreCase: true, out SameSiteMode result)
|
|
|
|
|
? result
|
|
|
|
|
: throw new ArgumentException($"The provided {nameof(sameSiteMode)} value could not be parsed into as SameSiteMode value.", nameof(sameSiteMode));
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public bool HasCookie(string cookieName) => GetCookieValue(cookieName) is not null;
|
2020-04-01 20:00:27 +02:00
|
|
|
}
|