-
Notifications
You must be signed in to change notification settings - Fork 10.3k
'GetAuthenticationStateAsync was called before SetAuthenticationState.' #17442
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
Comments
Thanks for contacting us. |
I switched to using |
You'll have to manually register the This is a little weird as I expected that this service is already registered behind the scenes. 😏 Update: Be aware not to use |
I get the same error when I try to access the |
I have the same problem. I have a service called my code public interface ICurrentUserService
{
Task<string> GetUserId();
}
public class CurrentUserService : ICurrentUserService
{
private readonly AuthenticationStateProvider _authenticationStateProvider;
public CurrentUserService(AuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider;
}
public async Task<string> GetUserId()
{
var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
return user.Identity.Name;
}
else
{
return null;
}
}
} my dbContext snapshot public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IApplicationDbContext
{
private readonly ICurrentUserService _currentUserService;
/* ... */
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, ICurrentUserService currentUserService) : base(options)
{
_currentUserService = currentUserService;
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
{
var userId = await _currentUserService.GetUserId();
foreach (var entry in ChangeTracker.Entries<AuditableEntity>())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.CreatedBy = userId;
entry.Entity.Created = DateTimeOffset.Now;
break;
case EntityState.Modified:
entry.Entity.LastModifiedBy = userId;
entry.Entity.LastModified = DateTimeOffset.Now;
break;
}
}
return await base.SaveChangesAsync(cancellationToken);
}
/* ... */
} I've noticed that when this service is called from a Blazor page then it works. |
I presume this is intended to be |
@nullpainter |
I've noticed that if I change |
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 For anyone else posting here, your issues may be different, since maybe you were not trying to inject |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
@Blackleones Did you ever get this to work? I'm doing basically the same thing and it's still not working. |
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):
|
Describe the bug
GetAuthenticationStateAsync throws an InvalidOperationException with the message:
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
The text was updated successfully, but these errors were encountered: