@page "/authentication/{action}"
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Microsoft.Extensions.Logging
@attribute [AllowAnonymous]
@layout EmptyLayout
@inject AuthenticationStateProvider _authenticationStateProvider
@inject ILogger<Authentication> Logger
<RemoteAuthenticatorView Action="@Action" OnLogInSucceeded="OnLogInSucceeded" OnLogOutSucceeded="OnLogOutSucceeded">
<LoggingIn>
<SplashScreen Text="Logging In..."></SplashScreen>
</LoggingIn>
<CompletingLoggingIn>
<SplashScreen Text="Completing Login"></SplashScreen>
</CompletingLoggingIn>
</RemoteAuthenticatorView>
@code {
[Parameter]
public string Action { get; set; }
private async Task OnLogInSucceeded(RemoteAuthenticationState remoteAuthenticationState)
{
Logger.LogInformation("{Component}: OnLoginSucceeded start", nameof(Authentication));
var authenticationState = await _authenticationStateProvider.GetAuthenticationStateAsync();
Logger.LogInformation("{Component}: authenticationState retrieved", nameof(Authentication));
if (authenticationState.User.Identity is not null && authenticationState.User.Identity.IsAuthenticated)
{
Logger.LogInformation("{Component}: user identity is not null and user is authenticated", nameof(Authentication));
if (authenticationState.User.Claims.All(c => c.Type != "role"))
{
Logger.LogInformation("{Component}: user claims does not have any role types setting return url of remoteAuthenticationState to unauthorized", nameof(Authentication));
remoteAuthenticationState.ReturnUrl = "/unauthorized";
}
}
Logger.LogInformation("{Component}: OnLoginSucceeded end", nameof(Authentication));
}
private void OnLogOutSucceeded(RemoteAuthenticationState remoteAuthenticationState)
{
Logger.LogInformation("{Component}: OnLogoutSucceeded start", nameof(Authentication));
remoteAuthenticationState.ReturnUrl = "/authentication/login";
Logger.LogInformation("{Component}: set remoteAuthenticationState Return Url to /authentication/login", nameof(Authentication));
Logger.LogInformation("{Component}: OnLogoutSucceeded end", nameof(Authentication));
}
}
This is my blazer code, client bookmarked the URL as
http://localhost:5300/authentication/login-callback#code=0.AVoAZNoWcXd0WUyrFv2LtmlwSUir_qkyXOROvH9M36rkDGrXALs.AgABBAIAAAApTwJmzXqdR4BN2miheQMYAgDs_wUA9P-
How can my application still support it, as of now it is staying in loading with spinner.
I have tried with
<RemoteAuthenticatorView Action="@Action" OnLogInSucceeded="OnLogInSucceeded" OnLogOutSucceeded="OnLogOutSucceeded">
<LoggingIn>
<SplashScreen Text="Logging In..."></SplashScreen>
</LoggingIn>
<CompletingLoggingIn>
<SplashScreen Text="Completing Login"></SplashScreen>
</CompletingLoggingIn>
</RemoteAuthenticatorView>
@code {
[Parameter]
public string Action { get; set; }
protected override async Task OnParametersSetAsync()
{
if (Action == "login-callback")
{
await Task.Delay(2000);
await CompleteLogin();
}
}
private async Task CompleteLogin()
{
var authenticationState = await _authenticationStateProvider.GetAuthenticationStateAsync();
if (authenticationState.User.Identity is not null && authenticationState.User.Identity.IsAuthenticated)
{
if (authenticationState.User.Claims.All(c => c.Type != "role"))
{
Navigation.NavigateTo("/unauthorized");
}
else
{
Navigation.NavigateTo("/");
}
}
else
{
Navigation.NavigateToLogin("/authentication/login");
}
}
}
I would like to is there any better way to achieve it or is it the right path.
New contributor
visweswar .penti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.