initial commit from an older template
This commit is contained in:
47
Services/Cookies/CookieService.cs
Executable file
47
Services/Cookies/CookieService.cs
Executable file
@@ -0,0 +1,47 @@
|
||||
using GamificationService.Exceptions.UtilServices.Cookies;
|
||||
|
||||
namespace GamificationService.Services.Cookies;
|
||||
|
||||
public class CookieService : ICookieService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly ILogger<CookieService> _logger;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public CookieService(IHttpContextAccessor httpContextAccessor, ILogger<CookieService> logger, IConfiguration configuration)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_logger = logger;
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public Task<bool> SetCookie(string key, string value, CookieOptions options)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Adding cookie {CookieKey} with value {CookieValue}", key, value);
|
||||
_httpContextAccessor.HttpContext.Response.Cookies.Append(key, value, options);
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to add cookie {CookieKey}", key);
|
||||
throw new SetCookiesException(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> RemoveCookie(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogDebug("Deleting cookie {CookieKey}", key);
|
||||
_httpContextAccessor.HttpContext.Response.Cookies.Delete(key);
|
||||
return await Task.FromResult(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to delete cookie {CookieKey}", key);
|
||||
throw new DeleteCookiesException(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user