2021-02-18 11:06:02 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
2020-06-02 13:28:30 +10:00
|
|
|
using System.Globalization;
|
2021-02-17 11:50:19 +01:00
|
|
|
using System.Security.Claims;
|
2020-06-02 13:28:30 +10:00
|
|
|
using System.Security.Principal;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Extensions
|
2020-06-02 13:28:30 +10:00
|
|
|
{
|
|
|
|
|
public static class AuthenticationExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that the thread culture is set based on the back office user's culture
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static void EnsureCulture(this IIdentity identity)
|
2020-08-04 12:54:54 +02:00
|
|
|
{
|
|
|
|
|
var culture = GetCulture(identity);
|
|
|
|
|
if (!(culture is null))
|
|
|
|
|
{
|
|
|
|
|
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = culture;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 13:24:35 +01:00
|
|
|
public static CultureInfo? GetCulture(this IIdentity identity)
|
2020-06-02 13:28:30 +10:00
|
|
|
{
|
2022-02-09 13:24:35 +01:00
|
|
|
if (identity is ClaimsIdentity umbIdentity && umbIdentity.VerifyBackOfficeIdentity(out _) && umbIdentity.IsAuthenticated && umbIdentity.GetCultureString() is not null)
|
2020-06-02 13:28:30 +10:00
|
|
|
{
|
2022-02-09 13:24:35 +01:00
|
|
|
return CultureInfo.GetCultureInfo(umbIdentity.GetCultureString()!);
|
2020-06-02 13:28:30 +10:00
|
|
|
}
|
2020-08-04 12:54:54 +02:00
|
|
|
|
|
|
|
|
return null;
|
2020-06-02 13:28:30 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|