// Copyright (c) Umbraco. // See LICENSE for more details. using System.Globalization; using System.Security.Claims; using System.Security.Principal; namespace Umbraco.Extensions; /// /// Extension methods for . /// public static class AuthenticationExtensions { /// /// Ensures that the thread culture is set based on the back office user's culture. /// /// The identity. public static void EnsureCulture(this IIdentity identity) { CultureInfo? culture = GetCulture(identity); if (culture is not null) { Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture = culture; } } /// /// Gets the culture string from the back office user. /// /// The identity. /// /// The culture string. /// public static string? GetCultureString(this IIdentity identity) { if (identity is ClaimsIdentity umbIdentity && umbIdentity.VerifyBackOfficeIdentity(out _) && umbIdentity.IsAuthenticated) { return umbIdentity.GetCultureString(); } return null; } /// /// Gets the culture from the back office user. /// /// The identity. /// /// The culture. /// public static CultureInfo? GetCulture(this IIdentity identity) { string? culture = identity.GetCultureString(); if (!string.IsNullOrEmpty(culture)) { return CultureInfo.GetCultureInfo(culture); } return null; } }