- Introduce EnvelopeCookieManager wrapper around ChunkingCookieManager to generate dynamic cookie names based on envelopeReceiverId or envKey. Ensures request/response cookies are scoped per envelope.
50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
namespace EnvelopeGenerator.Web;
|
|
|
|
public class EnvelopeCookieManager : ICookieManager
|
|
{
|
|
private readonly IEnumerable<string> _envelopeKeyBasedCookieNames;
|
|
|
|
private readonly ChunkingCookieManager _inner = new();
|
|
|
|
public EnvelopeCookieManager(params string[] envelopeKeyBasedCookieNames)
|
|
{
|
|
_envelopeKeyBasedCookieNames = envelopeKeyBasedCookieNames;
|
|
}
|
|
|
|
private string GetCookieName(HttpContext context, string key)
|
|
{
|
|
if (!_envelopeKeyBasedCookieNames.Contains(key))
|
|
return key;
|
|
|
|
var envId = context.GetRouteValue("envelopeReceiverId")?.ToString();
|
|
|
|
if (string.IsNullOrEmpty(envId) && context.Request.Query.TryGetValue("envKey", out var envKeyValue))
|
|
envId = envKeyValue;
|
|
|
|
if (string.IsNullOrEmpty(envId))
|
|
return key;
|
|
|
|
return $"{key}-{envId}";
|
|
}
|
|
|
|
public string? GetRequestCookie(HttpContext context, string key)
|
|
{
|
|
var cookieName = GetCookieName(context, key);
|
|
return _inner.GetRequestCookie(context, cookieName);
|
|
}
|
|
|
|
public void AppendResponseCookie(HttpContext context, string key, string? value, CookieOptions options)
|
|
{
|
|
var cookieName = GetCookieName(context, key);
|
|
_inner.AppendResponseCookie(context, cookieName, value, options);
|
|
}
|
|
|
|
public void DeleteCookie(HttpContext context, string key, CookieOptions options)
|
|
{
|
|
var cookieName = GetCookieName(context, key);
|
|
_inner.DeleteCookie(context, cookieName, options);
|
|
}
|
|
}
|