简体   繁体   中英

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

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM