-
Notifications
You must be signed in to change notification settings - Fork 25.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Companion article for BWA OIDC w/Aspire sample app #31265
Comments
This was referenced Jan 11, 2024
This was referenced Jan 22, 2024
This was referenced Jan 24, 2024
@travaille-dev ... Here's a revised @implements IDisposable
@using System.Reflection
@using System.Security.Claims
@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService
@inject NavigationManager NavigationManager
<div class="nav-item px-3">
<AuthorizeView>
<Authorized>
<form action="authentication/logout" method="post">
<AntiforgeryToken />
<input type="hidden" name="ReturnUrl" value="@currentUrl" />
<button type="submit" class="nav-link">
<span class="bi bi-arrow-bar-left-nav-menu" aria-hidden="true"></span> Logout @context.User.Identity?.Name
</button>
</form>
</Authorized>
<NotAuthorized>
<a class="nav-link" href="authentication/login">
<span class="bi bi-person-badge-nav-menu" aria-hidden="true"></span> Login
</a>
</NotAuthorized>
</AuthorizeView>
</div>
@code {
private string? currentUrl;
private ClaimsPrincipal? user;
[CascadingParameter]
private Task<AuthenticationState>? authenticationState { get; set; }
protected override async Task OnInitializedAsync()
{
currentUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.LocationChanged += OnLocationChanged;
if (authenticationState is not null)
{
var authState = await authenticationState;
user = authState?.User;
}
}
private void OnLocationChanged(object? sender, LocationChangedEventArgs e)
{
var currentUrl = NavigationManager.ToBaseRelativePath(e.Location);
if (user?.Identity is not null && user.Identity.IsAuthenticated)
{
var matchingAuthComponentCount =
Assembly.GetExecutingAssembly()
.ExportedTypes
.Where(t => t.IsSubclassOf(typeof(ComponentBase)))
.Any(c => c.GetCustomAttributes(inherit: true)
.OfType<AuthorizeAttribute>()
.Any() &&
c.GetCustomAttributes(inherit: true)
.OfType<RouteAttribute>()
.Where(d => d.Template == $"/{currentUrl}")
.Any());
if (matchingAuthComponentCount)
{
currentUrl = string.Empty;
}
}
StateHasChanged();
}
public void Dispose()
{
NavigationManager.LocationChanged -= OnLocationChanged;
}
} |
This was referenced Jan 26, 2024
UPDATE (1/26) ...
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/dotnet/blazor-samples/tree/main/8.0/BlazorWebAppOidc
Change the sample folder name first.Done! ...BlazorWebAppOidc
The text was updated successfully, but these errors were encountered: