Authinator/Backend/Utils/CookieUtils.cs
2023-06-01 06:14:24 +02:00

36 lines
1.2 KiB
C#

using System.Globalization;
using Microsoft.Extensions.Primitives;
using Microsoft.Net.Http.Headers;
namespace Authinator.Backend.Utils;
public static class CookieUtils {
public static void AppendUnencodedCookie(this HttpResponse response, string key, string value) {
response.Cookies.Delete(key);
var setCookieHeaderValue = new SetCookieHeaderValue(key, value.Replace(" ", "+"));
response.Headers[HeaderNames.SetCookie] = StringValues.Concat(response.Headers[HeaderNames.SetCookie], setCookieHeaderValue.ToString());
}
public static void AppendUnencodedCookie(this HttpResponse response, string key, string value, CookieOptions options) {
if (options == null) {
throw new ArgumentNullException(nameof(options));
}
response.Cookies.Delete(key);
var setCookieHeaderValue = new SetCookieHeaderValue(key, value.Replace(" ", "+")) {
Domain = options.Domain,
Path = options.Path,
Expires = options.Expires,
MaxAge = options.MaxAge,
Secure = options.Secure,
SameSite = (Microsoft.Net.Http.Headers.SameSiteMode)options.SameSite,
HttpOnly = options.HttpOnly
};
response.Headers[HeaderNames.SetCookie] = StringValues.Concat(response.Headers[HeaderNames.SetCookie], setCookieHeaderValue.ToString());
}
}