1

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.