Blazor WASM JWT - Wie man den Auth -Status im Mainlayout bekommt
Posted: 21 Mar 2025, 07:39
Ich wollte eine Authentifizierung in Blazor WebAssembly, JWT, Net 8 einrichten, aber ich habe ein Problem gestoßen. Wenn ich Tags in eine interaktive Komponente platziere, wie hier: < /p>
Code: Select all
@page "/"
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
@inject IStringLocalizer Localizer
@using Microsoft.AspNetCore.Components.Authorization
Authorized
NotAuthorized
< /code>
Mein AuthentifizierungStateProvider funktioniert gut und gibt autorisiert oder nothorisiert zurück. Aber wenn ich versuche, es in mainlayout.razor zu tun, wie hier: < /p>
@using Microsoft.AspNetCore.Components.Authorization
@inherits LayoutComponentBase
Authorized
NotAuthorized
< /code>
Ich werde immer notauthorisiert. Dies geschieht, weil die Anwendung in mainlayout.razor nicht interaktiv gerendert wird. Wie kann ich das beheben?using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.JSInterop;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
namespace WMS.Client.Providers;
public class JwtAuthenticationStateProvider(ILocalStorageService LocalStorage, IJSRuntime JSRuntime) : AuthenticationStateProvider
{
private readonly ILocalStorageService _localStorage = LocalStorage;
private readonly IJSRuntime _JSRuntime = JSRuntime;
private const string TokenKey = "authToken";
public sealed override async Task GetAuthenticationStateAsync()
{
if (_JSRuntime is not IJSInProcessRuntime)
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
var token = await _localStorage.GetItemAsync(TokenKey);
if (string.IsNullOrEmpty(token))
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
var user = new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(token), "jwt"));
return new AuthenticationState(user);
}
public async Task Login(string token)
{
await _localStorage.SetItemAsync(TokenKey, token);
var user = new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(token), "jwt"));
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(user)));
}
public async Task Logout()
{
await _localStorage.RemoveItemAsync(TokenKey);
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()))));
}
private static IEnumerable ParseClaimsFromJwt(string token)
{
var handler = new JwtSecurityTokenHandler();
var jwt = handler.ReadJwtToken(token);
return jwt.Claims;
}
}
< /code>
meine Routen.Razor: < /p>
@using Microsoft.AspNetCore.Components.Authorization
Sorry, there's nothing at this address.
< /code>
app.razor:
< /code>
program.cs in client Seite: < /p>
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using System.Globalization;
using WMS.Client.Providers;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddLocalization(options => { options.ResourcesPath = "Resources"; });
builder.Services.AddAuthorizationCore();
builder.Services.AddScoped();
builder.Services.AddScoped();
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
await builder.Build().RunAsync();
< /code>
Program.cs in der Serverseite: < /p>
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.Authorization;
using WMS.Client.Providers;
using WMS.Components;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveWebAssemblyComponents();
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddAuthorization();
builder.Services.AddScoped();
builder.Services.AddScoped();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
app.UseStaticFiles();
app.UseAntiforgery();
app.MapRazorComponents()
.AddInteractiveWebAssemblyRenderMode()
.AddAdditionalAssemblies(typeof(WMS.Client._Imports).Assembly);
app.Run();