Files
Umbraco-CMS/src/Umbraco.Web.Common/AspNetCore/AspNetCoreSessionManager.cs
Andy Butland 96d33201aa Few more NRT tweaks (#12323)
* Amended GetAll() on IDataTypeService to return an empty collection rather than null.

* Added a ClearSessionValue method to ISessionManager (given you can no longer set a value to null).

* Allow for null values in a StatefulNotification.

* Removed obsoletion of synchronous messages on TreeControllerBase.

* Fixed further CS8620 warnings in core project.

* Further fix to nullable warning.

* Aligned nullablility of retreiving tree nodes and menus, synchronously or asynchronously (such that we no longer can get null values, always empty collection objects).
2022-05-01 08:18:09 +02:00

66 lines
1.7 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Umbraco.Cms.Core.Net;
using Umbraco.Cms.Core.Web;
namespace Umbraco.Cms.Web.Common.AspNetCore
{
internal class AspNetCoreSessionManager : ISessionIdResolver, ISessionManager
{
private readonly IHttpContextAccessor _httpContextAccessor;
public AspNetCoreSessionManager(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
/// <summary>
/// If session isn't enabled this will throw an exception so we check
/// </summary>
private bool IsSessionsAvailable => !(_httpContextAccessor.HttpContext?.Features.Get<ISessionFeature>() is null);
public string? SessionId
{
get
{
HttpContext? httpContext = _httpContextAccessor?.HttpContext;
return IsSessionsAvailable
? httpContext?.Session?.Id
: "0";
}
}
public string? GetSessionValue(string key)
{
if (!IsSessionsAvailable)
{
return null;
}
return _httpContextAccessor.HttpContext?.Session.GetString(key);
}
public void SetSessionValue(string key, string value)
{
if (!IsSessionsAvailable)
{
return;
}
_httpContextAccessor.HttpContext?.Session.SetString(key, value);
}
public void ClearSessionValue(string key)
{
if (!IsSessionsAvailable)
{
return;
}
_httpContextAccessor.HttpContext?.Session.Remove(key);
}
}
}