You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Although I log in to the application as a user, the calls to the APIs work only if no authorisation is required in the Minimal API. It is like that the IHttpClientFactory doesn't have the
How can I protect the APIs and use them from the client side?
What I tried
In the Program.cs of the server, I added this code for the HttpClient:
When I run the application, I immediately get an error:
System.AggregateException: 'Some services are not able to be
constructed (Error while validating the service descriptor
'ServiceType: MyApp.CustomAuthorizationMessageHandler
Lifetime: Scoped ImplementationType:
MyApp.CustomAuthorizationMessageHandler': Unable to resolve
service for type
'Microsoft.AspNetCore.Components.WebAssembly.Authentication.IAccessTokenProvider'
while attempting to activate
'MyApp.CustomAuthorizationMessageHandler'.)'
Update
I added the configuration of IHttpClientFactory in the client project too.
But the result has not changed. When from the UI, there is a call to the API, I always get 200 as an HTTP code but the content of the result is a login page.
Just to give you more info, all the calls are in a different project and I reference this project in the client project.
The text was updated successfully, but these errors were encountered:
In my .NET 8 Blazor project, I added the "Individual Account" to manage users. The application has 2 projects.
Server
The server side contains the login part and the controllers/endpoints.
The controllers/endpoints are minimal APIs like this one
in order to save in the database. Also, I added the configuration for
HttpClient
:Client
The client project has all the views. The views call the APIs provided by the server.
In the Program.cs this configuration is added out of the box:
In order to connect to the APIs from the pages, I created a class to group all the calls in a single place. The constructor is this
Although I log in to the application as a user, the calls to the APIs work only if no authorisation is required in the Minimal API. It is like that the
IHttpClientFactory
doesn't have theHow can I protect the APIs and use them from the client side?
What I tried
In the
Program.cs
of the server, I added this code for theHttpClient
:The
CustomAuthorizationMessageHandler
is as followsI'm doing this because I want to make calls to the APIs exposed in the server project protected by the Individual Account. So, in a page
When I run the application, I immediately get an error:
Update
I added the configuration of
IHttpClientFactory
in the client project too.But the result has not changed. When from the UI, there is a call to the API, I always get 200 as an HTTP code but the content of the result is a login page.
Just to give you more info, all the calls are in a different project and I reference this project in the client project.
The text was updated successfully, but these errors were encountered: