Home > other >  AuthenticationStateProvider couldn't inject in ASP.NET Core Web App
AuthenticationStateProvider couldn't inject in ASP.NET Core Web App

Time:09-22

I have a ASP.NET Core Web App project based on .NET 6. It has .cshtml and it's model. I was trying to inject AuthenticationStateProvider in the constructor of the page model as below

public class ProfileModel : PageModel
{
            
       public AuthenticationStateProvider AuthenticationStateProvider { get; set; }

       public ProfileModel(AuthenticationStateProvider AuthenticationStateProvider)
       {

It throws error telling

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider' while attempting to activate 'OKTAIntegration.Pages.ProfileModel'.

This injection works well with Blazor server application. So question is that "AuthenticationStateProvider" class only supports in Blazor and not in usual ASP.NET Core Web App projects? Then how I get below similar authentication state in ASP.NET Core Web App project, for which I needed AuthenticationStateProvider class.

var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();

CodePudding user response:

No need to inject it, PageModel exposes User property which can be used to access the authenticated user info if any is present.

  • Related