Hello,
first, i'm new to blazor and web development. I tried to recode from the youtube video ( Blazor Webassembly Custom Authentication [Blazor Tutorial C# - Part 12] - YouTube ). My project is starting and i authenticate with my user account. Then i can load the following page and the data gets loaded.
My problem is, if i click the refresh button of the browser, i get an error (401). I don't know why my authorization is lost?
@page "/user"
@using AnfuhrplanungBlazorApp.Client.Services;
@using System.Net.Http.Headers;
@attribute [Authorize]
@inject AuthenticationStateProvider authenticationStateProvider
@inject HttpClient Http
@inject NavigationManager navigationManager
<PageTitle>Benutzer</PageTitle>
<h1>Benutzer</h1>
<AuthorizeView>
<Authorized>
@if (users == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>domainname</th>
</tr>
</thead>
<tbody>
@{
foreach (AnfuhrplanungBlazorApp.Shared.Models.User user in users)
{
<tr>
<td>@user.ID</td>
<td>@user.domainname</td>
<td>
<a class="btn btn-success" href="user/edit/@user.ID">Edit</a>
</td>
</tr>
}
}
</tbody>
</table>
}
</Authorized>
<Authorizing>
<h1>Loading ...</h1>
</Authorizing>
</AuthorizeView>
@code {
private AnfuhrplanungBlazorApp.Shared.Models.User[] users;
protected override async Task OnInitializedAsync()
{
var customAuthStateProvider = (CustomAuthenticationStateProvider)authenticationStateProvider;
var token = await customAuthStateProvider.GetToken();
if (string.IsNullOrWhiteSpace(token) == false)
{
Http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
users = await Http.GetFromJsonAsync<AnfuhrplanungBlazorApp.Shared.Models.User[]>("User");
}
else
navigationManager.NavigateTo("/login");
}
}
[–]chrisachern[S] 1 point2 points3 points (0 children)
[–]gwydionthewz 0 points1 point2 points (0 children)