question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'GetAuthenticationStateAsync was called before SetAuthenticationState.'

See original GitHub issue

Describe the bug

GetAuthenticationStateAsync throws an InvalidOperationException with the message:

'GetAuthenticationStateAsync was called before SetAuthenticationState.'

I do not know if this is an actual bug or just a lack of documentation on its correct use.

ref: https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.server.serverauthenticationstateprovider?view=aspnetcore-3.0

To Reproduce

Inject a ServerAuthenticationStateProvider into a page. Then call 'GetAuthenticationStateAsync in any of its async-events.

Further technical details

<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TimBurriscommented, Jun 13, 2020

Much like @eddiepaz , i’m still exeriencing the same issue. i think for me it’s because i’m using custom Middleware and utilizing IServiceProvider with CreateScope to created a Scoped instance of my User provider.

the error can be demonstrated with the following (serviceProvider is an injected IServiceProvider):

using (var serviceScope = serviceProvider.CreateScope())
{
    var service = serviceScope.ServiceProvider.GetRequiredService<AuthenticationStateProvider>();
    var state= await service.GetAuthenticationStateAsync();
}
1reaction
SteveSandersonMScommented, Jun 4, 2020

Inject a ServerAuthenticationStateProvider into a page.

This is the problem. Don’t do that, because you’ll just receive an uninitialized instance.

You need to use the instance provided by the framework, which has been initialized. To get that, inject an AuthenticationStateProvider instead of ServerAuthenticationStateProvider.

For anyone else posting here, your issues may be different, since maybe you were not trying to inject ServerAuthenticationStateProvider. If you still have trouble, could you please post a separate issue with repro steps for your scenario? Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Exception 'GetAuthenticationStateAsync was called before ...
Describe the bug In my Blazor Server app, exactly as described in this bug report, 'GetAuthenticationStateAsync was called before SetAuthenticationState.
Read more >
Error "GetAuthenticationStateAsync was called before ...
GetAuthenticationStateAsync () to call GetAuthenticationStateAsync before SetAuthenticationState. So the error will accour.
Read more >
GetAuthenticationStateAsync was Called before ...
Error Message. GetAuthenticationStateAsync was called before SetAuthenticationState. Cause\Possible Cause(s). The Telerik Reporting REST Service is recommended ...
Read more >
How to fix "GetAuthenticationStateAsync was called before ...
Now getting the error: GetAuthenticationStateAsync was called before SetAuthenticationState. at Microsoft.AspNetCore.Components.Server.
Read more >
What is Blazor? A Tutorial on Building Web Apps with ...
... would need this as AddScoped which comes back with an error of : 'GetAuthenticationStateAsync was called before SetAuthenticationState.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found