-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for roles, culture, timezone and email
- only doc and demo changes
- Loading branch information
Showing
9 changed files
with
178 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 12 additions & 6 deletions
18
demos/BlazorWebApp/BlazorWebApp.Client/Pages/Counter.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
@page "/counter" | ||
@rendermode InteractiveAuto | ||
@using System.Globalization | ||
|
||
<PageTitle>Counter</PageTitle> | ||
|
||
<h1>Counter</h1> | ||
<h1>Counter @@ @RendererInfo.Name</h1> | ||
|
||
<p role="status">Current count: @currentCount</p> | ||
|
||
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | ||
|
||
<p> | ||
Culture: <span class="">@CultureInfo.CurrentCulture.Name @CultureInfo.CurrentUICulture.Name</span> | ||
</p> | ||
|
||
|
||
@code { | ||
private int currentCount = 0; | ||
private int currentCount = 0; | ||
|
||
private void IncrementCount() | ||
{ | ||
currentCount++; | ||
} | ||
private void IncrementCount() | ||
{ | ||
currentCount++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||
using System.Globalization; | ||
|
||
var builder = WebAssemblyHostBuilder.CreateDefault(args); | ||
|
||
builder.Services.AddAuthorizationCore(); | ||
builder.Services.AddCascadingAuthenticationState(); | ||
builder.Services.AddAuthenticationStateDeserialization(); | ||
builder.Services.AddAuthenticationStateDeserialization(o=>o.DeserializationCallback = ProcessLanguageAndCultureFromClaims(o.DeserializationCallback)); | ||
|
||
static Func<AuthenticationStateData?, Task<AuthenticationState>> ProcessLanguageAndCultureFromClaims(Func<AuthenticationStateData?, Task<AuthenticationState>> authenticationStateData) => | ||
state => { | ||
var tsk = authenticationStateData(state); | ||
if (!tsk.IsCompletedSuccessfully) return tsk; | ||
var authState = tsk.Result; | ||
if (authState?.User is not { } user) return tsk; | ||
var userCulture = user.FindFirst("culture")?.Value; | ||
//Console.WriteLine($"New culture = {userCulture ?? "unset"}. Old = {CultureInfo.DefaultThreadCurrentCulture?.Name ?? "unset"}"); | ||
var userUiCulture = user.FindFirst("locale")?.Value ?? userCulture; | ||
//Console.WriteLine($"New locale = {userUiCulture ?? "unset"}. Old = {CultureInfo.DefaultThreadCurrentUICulture?.Name ?? "unset"}"); | ||
if (userUiCulture == null) return tsk; | ||
|
||
CultureInfo.DefaultThreadCurrentCulture = new(userCulture ?? userUiCulture); | ||
CultureInfo.DefaultThreadCurrentUICulture = new(userUiCulture); | ||
return tsk; | ||
}; | ||
|
||
await builder.Build().RunAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
demos/BlazorWebApp/BlazorWebApp/DemoUsage/OidcClaimsCultureProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Localization; | ||
|
||
namespace BlazorWebApp.DemoUsage; | ||
|
||
/// <summary> | ||
/// Pull the "locale" and "culture" claims from the user and use that as the uiCulture and culture. | ||
/// If either is missing, the other is used as a fallback. If both are missing, or the locale is not in the list of known cultures, it does nothing. | ||
/// <example><code> | ||
/// host.UseRequestLocalization(o => { | ||
/// var cultures = ...; | ||
/// o.AddSupportedCultures(cultures) | ||
/// .AddSupportedUICultures(cultures) | ||
/// .SetDefaultCulture(cultures[0]); | ||
/// //insert before the final default provider (the AcceptLanguageHeaderRequestCultureProvider) | ||
/// o.RequestCultureProviders.Insert(o.RequestCultureProviders.Count - 1, new OidcClaimsCultureProvider {Options = o}); | ||
/// }); | ||
/// </code></example> | ||
/// </summary> | ||
public class OidcClaimsCultureProvider : RequestCultureProvider | ||
{ | ||
///<inheritdoc/> | ||
public override Task<ProviderCultureResult?> DetermineProviderCultureResult(HttpContext httpContext) => Task.FromResult(GetCultureFromClaims(httpContext)); | ||
|
||
private static ProviderCultureResult? GetCultureFromClaims(HttpContext ctx) | ||
{ | ||
var userCulture = ctx.User.FindFirstValue("culture"); | ||
var userUiCulture = ctx.User.FindFirstValue("locale") ?? userCulture; | ||
if (userUiCulture == null) goto noneFound; | ||
|
||
return new(userCulture ?? userUiCulture, userUiCulture); | ||
noneFound: | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters