35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using Microsoft.AspNetCore.Localization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Prefab.Culture;
|
|
|
|
/// <summary>
|
|
/// Controller for setting the culture.
|
|
/// </summary>
|
|
[Route("[controller]/[action]")]
|
|
public class CultureController : Controller
|
|
{
|
|
/// <summary>
|
|
/// Set the culture and redirect to the specified URL.
|
|
/// </summary>
|
|
/// <param name="culture"></param>
|
|
/// <param name="redirectUri"></param>
|
|
/// <returns>LocalRedirect ActionResult</returns>
|
|
/// <remarks>
|
|
/// Use LocalRedirect action result to prevent open redirect attacks.
|
|
/// For more information <see href="https://learn.microsoft.com/en-us/aspnet/core/security/preventing-open-redirects?view=aspnetcore-9.0">Prevent open redirect attacks in ASP.NET Core</see>
|
|
/// </remarks>
|
|
public IActionResult Set(string? culture, string redirectUri)
|
|
{
|
|
if (culture != null)
|
|
{
|
|
HttpContext.Response.Cookies.Append(
|
|
CookieRequestCultureProvider.DefaultCookieName,
|
|
CookieRequestCultureProvider.MakeCookieValue(
|
|
new RequestCulture(culture, culture)));
|
|
}
|
|
|
|
return LocalRedirect(redirectUri);
|
|
}
|
|
}
|