Blazor Samples - How to send HTTP requests to the anonymously accessible endpoint without access token on an authentication enabled Blazor Wasm App?
This is a sample code repository of a Blazor Wasm app.
This repository describes how to send HTTP requests to the anonymously accessible endpoint without access token on an authentication enabled Blazor Wasm App.
The master
branch reproduces the problem what this repository will describe.
If you clone this repository on your computer, check out the master
branch, and execute "dotnet run", you will see AccessTokenNotAvailableException
error.
This error causes when sending HTTP requests from the Blazor Wasm App to the Web API endpoint live in it's hosted server process before signed in, even though that endpoint is allowed anonymous access.
Because the Blazor app which was created from the default project template with the "Authentication" option enabled always tries to attach an access token to any HTTP requests whether the user is signed in or not.
The soulution1
branch resolved the issue above, by configuring "AuthorizationMessageHanlder" to allow attaching an access token to only selected endpoint under the specified subpath.
The soulution2
branch also resolved that issue.
This solution explicitly gets a named HTTP client which is configured to attach an access token from "IHttpClientFactory", when sending HTTP requests to authorization required endpoint.
The soulution3
branch is another solution of opposite to "solution2".
This solution explicitly gets a named HTTP client which is plain one from "IHttpClientFactory", when sending HTTP requests to anonymous access allowed endpoint.