Code
·
28 lines
·
801 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace GitServer.Extensions;
public static class EndpointExtensions
{
public static IEndpointRouteBuilder MapSetLanguage(this IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("/set-language", (string? lang, string? returnUrl, HttpResponse response) =>
{
if (!string.IsNullOrEmpty(lang) && lang.Length <= 10 && lang.All(c => char.IsLetterOrDigit(c) || c == '-'))
{
response.Cookies.Append("lang", lang, new CookieOptions
{
Expires = DateTimeOffset.UtcNow.AddYears(1),
IsEssential = true,
SameSite = SameSiteMode.Lax,
HttpOnly = true
});
}
var redirect = string.IsNullOrEmpty(returnUrl) ? "/" : returnUrl;
return Results.Redirect(redirect);
});
return endpoints;
}
}