Files
Umbraco-CMS/src/Umbraco.Web.Common/AspNetCore/AspNetCoreCookieManager.cs
Elitsa Marinovska 295f6f8720 V14: Backend changes to facilitate Preview mode in Bellissimma (#16279)
* Sends GUID instead of the numeric ID for SignalR Preview Hub

* Add possibility to set cookies as HttpOnly

* Set UMB_PREVIEW cookie as HttpOnly

* fixup! Add possibility to set cookies as HttpOnly

* Refactor ContentFinderByIdPath to more readable

* Create ContentFinderByKeyPath reusing logic from ContentFinderByIdPath

* Add a comment to DisableFindContentByIdPath setting

* Append new content finder

* Change ordering of content finders registrations

* Refactor with a base class

* Update/refactor and add tests regarding ContentFindersByIdentifier

* Fix comment

* Avoiding breaking change

* Make usages use non-obsolete implementation

* Fixed todo in config instead of use the one old legacy name even more. Also obsoleted the ContentFinderByIdPath

* add `preview` as an allowed backoffice client route

---------

Co-authored-by: Sven Geusens <sge@umbraco.dk>
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
Co-authored-by: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com>
2024-05-16 15:53:42 +02:00

35 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Http;
using Umbraco.Cms.Core.Web;
namespace Umbraco.Cms.Web.Common.AspNetCore;
public class AspNetCoreCookieManager : ICookieManager
{
private readonly IHttpContextAccessor _httpContextAccessor;
public AspNetCoreCookieManager(IHttpContextAccessor httpContextAccessor) =>
_httpContextAccessor = httpContextAccessor;
public void ExpireCookie(string cookieName)
{
HttpContext? httpContext = _httpContextAccessor.HttpContext;
if (httpContext is null)
{
return;
}
var cookieValue = httpContext.Request.Cookies[cookieName];
httpContext.Response.Cookies.Append(cookieName, cookieValue ?? string.Empty,
new CookieOptions { Expires = DateTime.Now.AddYears(-1) });
}
public string? GetCookieValue(string cookieName) => _httpContextAccessor.HttpContext?.Request.Cookies[cookieName];
public void SetCookieValue(string cookieName, string value, bool httpOnly) =>
_httpContextAccessor.HttpContext?.Response.Cookies.Append(cookieName, value, new CookieOptions { HttpOnly = httpOnly });
public bool HasCookie(string cookieName) => !(GetCookieValue(cookieName) is null);
}