Skip to content

Commit

Permalink
Unify CookieOptions config for AuthUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Dec 19, 2024
1 parent aa3700c commit d47ee88
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions Common/Utils/AuthUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,26 @@ public static class AuthUtils
"Device-Token"
];

public static void SetSessionKeyCookie(this HttpContext context, string sessionKey, string domain)
private static CookieOptions GetCookieOptions(string domain, TimeSpan lifetime)
{
context.Response.Cookies.Append(AuthConstants.UserSessionCookieName, sessionKey, new CookieOptions
return new CookieOptions
{
Expires = new DateTimeOffset(DateTime.UtcNow.Add(Duration.LoginSessionLifetime)),
Expires = new DateTimeOffset(DateTime.UtcNow.Add(lifetime)),
Secure = true,
HttpOnly = true,
SameSite = SameSiteMode.Strict,
Domain = domain
});
};
}

public static void SetSessionKeyCookie(this HttpContext context, string sessionKey, string domain)
{
context.Response.Cookies.Append(AuthConstants.UserSessionCookieName, sessionKey, GetCookieOptions(domain, Duration.LoginSessionLifetime));
}

public static void RemoveSessionKeyCookie(this HttpContext context, string domain)
{
context.Response.Cookies.Append(AuthConstants.UserSessionCookieName, string.Empty, new CookieOptions
{
Expires = DateTime.Now.AddDays(-1),
Secure = true,
HttpOnly = true,
SameSite = SameSiteMode.Strict,
Domain = domain
});
context.Response.Cookies.Append(AuthConstants.UserSessionCookieName, string.Empty, GetCookieOptions(domain, TimeSpan.FromDays(-1)));
}

public static bool TryGetUserSession(this HttpContext context, [NotNullWhen(true)] out string? sessionToken)
Expand Down

0 comments on commit d47ee88

Please sign in to comment.