[Blazor Wasm AD B2C] Authentication OnLogInSucceeded event fired multiple times.
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
There seems to be a few related issues:
- By default the same token network request with
scope: openid profile offline_access, grant_type: refresh_token
is being called twice (is this necessary?). The authentication response time is already quite slow so this is pretty painful. - By default the
OnLogInSucceeded
event inRemoteAuthenticatorView
is firing twice. - Passing
[CascadingParameter] public Task<AuthenticationState>? AuthenticationState { get; set; }
causesOnLogInSucceeded
to fire three times. Is there a better way of retrieving the user claims here? - For application insights we need to set the AuthenticatedUserContext. By calling
SetAuthenticatedUserContext
theOnLoginSucceeded
is being called four times!
Full code:
<RemoteAuthenticatorView Action="@Action" OnLogInSucceeded="OnLogInSucceeded" OnLogOutSucceeded="OnLogOutSucceeded" >
<LogInFailed>
<ErrorDisplay Title="Sorry, your login failed." RetryText="logging in again" RetryAction="RedirectToLogin"
OnError="@(() => LogError("LogInFailed"))" />
</LogInFailed>
<LogOutFailed>
<ErrorDisplay Title="Sorry, log out operation failed." RetryText="refreshing the page" RetryAction="RefreshPage"
OnError="@(() => LogError("LogOutFailed"))" />
</LogOutFailed>
</RemoteAuthenticatorView>
@code{
[Parameter] public string? Action { get; set; }
[CascadingParameter] public Task<AuthenticationState>? AuthenticationState { get; set; }
private async Task OnLogInSucceeded()
{
Console.WriteLine("onlogin");
var user = (await AuthenticationState!).User;
await AppInsights.SetAuthenticatedUserContext(user.Id(), storeInCookie: true);
}
private async Task OnLogOutSucceeded()
{
Console.WriteLine("onlogout");
await AppInsights.ClearAuthenticatedUserContext();
}
Expected Behavior
scope: openid profile offline_access, grant_type: refresh_token
token network request to be called onceOnLoginSucceeded
to only be called once
Steps To Reproduce
Blazor WebAssembly standalone with Azure AD B2C
Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Using RemoteAuthenticatorView OnLogInSucceeded gives ...
For this application I use Azure B2C to auth, and I'm using redirect ... The event can trigger multiple times when being redirected...
Read more >Using RemoteAuthenticatorView OnLogInSucceeded gives a ...
Thanks to Zack Blazor Webassembly Authenticated Event, I was sent in a ... can trigger multiple times when being redirected from the Authentication...
Read more >Blazor WebAssembly Hosted app using Azure AD B2C
I have a Blazor WebAssembly Hosted C# application (.NET 6.0) using Azure AD B2C. On the B2C login screen, if the user clicks...
Read more >Secure a hosted ASP.NET Core Blazor WebAssembly app ...
This article explains how to create a hosted Blazor WebAssembly solution that uses Azure Active Directory (AAD) B2C for authentication.
Read more >Blazor Azure B2C Authentication and Authorization - YouTube
Blazor Azure B2C Authentication and Authorization. 8.4K views · 1 year ago #dotnet ... Blazor WebAssembly authentication with Azure AD.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Even if this issue won’t be fixed for .NET 7, the least Microsoft could do is provide some guidance on a workaround or recommended best practice to mitigate this issue. A list of do’s and don’ts even? Or an alternative way to do post-login work if there is one?
Anything would be helpful for this rather than leaving us out to dry on this because this can’t possibly be an edge use case for most developers.
This needs to be fixed sooner than .NET 7 planning stage. We are having this exact same issue. .NET 6.0.2 using Azure AD B2C. OnLoginSucceeded fires multiple times, which means that some post-login work we want to do can happen multiple times. It should happen just once and be dependable to fire only once, especially if we need to do any long running tasks during login like downloading some information from a Web API after the user has authenticated successfully.
Right now, I have to check the nonce value that comes back on the AdditionalProperties dictionary of the RemoteUserAccount and compare it to previous calls to know if I have done work before.
Lastly, shouldn’t OnLoginSucceeded complete before the application tries to initialize a destination page it is trying to navigate to?