-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Access AuthenticationStateProvider
in outgoing request middleware
#52379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
AuthenticationStateProvider
in outgoing request middleware
@MackinnonBuck Could this be because CreateInboundActivityHandler is not being called when it should be? @rsandbach pointed out that #51934 and #52390 could be related. |
Thanks for contacting us. We're moving this issue to the |
@ggomarighetti, the problem may have been caused by #51934, which is getting addressed in the February servicing release for .NET 8. Would you be able to try the new release in February and see if it resolves the issue? |
@MackinnonBuck no problem, as soon as the new version is available, I will run the test again and report the news in the issue. Thank you very much! |
Similarly, if this change allows arbitrary assemblies to once again access the user identity in Blazor I'll let you know. I don't know how we're supposed to build real apps without access to the current user's roles, claims, etc. |
@MackinnonBuck I have tested with the new versions and the issue has not been solved. |
By looking at this, I managed to get the right DI scope from within my I'm basically putting the access token in a scoped service from within the See also this for an example with |
Currently I no longer use Blazor for development. For those who read this message later, the only way to apply authentication and authorization correctly for an application with Blazor is to use a BFF pattern. Investigate the libraries or tutorials that exist at the time you read this, but, in principle, it is based on developing the Blazor application in client mode (Web Assembly) and securing everything with an ASP.NET REST API on top. This way you can connect the API to your authentication provider (in my case it was with Auth0) and be able to refresh the access token (in my case it was short lifetime) in an effective and efficient way. Honorable mention to @rockfordlhotka's comment, I don't know how they expect real applications to be developed with a framework in this state. I have currently migrated the project to TypeScript with NextJS. If necessary I can keep the issue open until the problem is solved, another way to do it is found or a library is developed that can solve it. Regards! |
The only thing that has worked for me is using the pre-8.0 way described here. Having to make every component that performs API calls inherit from the custom component base obviously isn't great and even then it sometimes just breaks for reasons I don't understand (even though I am able to work around it). The new approach added to 8.0 simply does not work, services are always null. Also, CreateInboundActivityHandler never gets called. |
Any updates on that? |
@arkiaconsulting Is it still working do you have an exemple code? |
@adampaquette I'm sorry but I didn't work with Blazor since many months |
Tested yesterday, and yes, you cannot access a scoped service in a DelegatingHandler. I finished with a base httpclient protected virtual async Task<HttpResponseMessage> SendAsync(Func<Task<HttpResponseMessage>> httpRequest)
{
return await httpRequest();
} overriden in child like this: protected override async Task<HttpResponseMessage> SendAsync(Func<Task<HttpResponseMessage>> httpRequest)
{
await SetSecruityHeaderAsync();
return await base.SendAsync(httpRequest);
}
private async Task SetSecruityHeaderAsync()
{
var usertoken = await _userService.GetCurrentUserTokenAsync();
if (string.IsNullOrEmpty(usertoken))
throw new ApplicationException("Cannot retrieve info, refresh your app");
Client.DefaultRequestHeaders.Clear();
Client.DefaultRequestHeaders.Add("Authorization", $"Bearer {usertoken}");
} And here no problemn to access my scoped "UserService" ... Can confirm it doesn't work to do the same |
Unfortuantely, we no longer expect that we'll get to this in .NET 10. |
I have done this painful process already and found a couple of really hacky workarounds. Guess I really have to write this blog series after all, now that we will not get any improvement in .Net 10 in this area. |
Is there an existing issue for this?
Describe the bug
I try to access AuthenticationStateProvider from a DelegatingHandler which intercepts requests from an HttpClient, following the official documentation guidelines:
But I get errors when implementing it in a completely new Blazor project (Server Side) in .NET 8 SDK.
Expected Behavior
I am looking to access the AuthenticationStateProvider from the DelegateHandler.
Steps To Reproduce
Program.cs
SecurityServiceAccessor.cs
SecurityCircuitHandler.cs
SecurityHandler.cs
Home.razor
Exceptions (if any)
.NET Version
8.0.100
Anything else?
SDK DE .NET:
Version: 8.0.100
Commit: 57efcf1350
Workload version: 8.0.100-manifests.6a1e483a
Entorno de tiempo de ejecución:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100\
Cargas de trabajo de .NET instaladas:
Workload version: 8.0.100-manifests.6a1e483a
[ios]
Origen de la instalación: VS 17.8.34316.72
Versión del manifiesto: 17.0.8478/8.0.100
Ruta de acceso del manifiesto: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.0.8478\WorkloadManifest.json
Tipo de instalación: Msi
[maui-windows]
Origen de la instalación: VS 17.8.34316.72
Versión del manifiesto: 8.0.3/8.0.100
Ruta de acceso del manifiesto: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json
Tipo de instalación: Msi
[android]
Origen de la instalación: VS 17.8.34316.72
Versión del manifiesto: 34.0.43/8.0.100
Ruta de acceso del manifiesto: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.43\WorkloadManifest.json
Tipo de instalación: Msi
[maccatalyst]
Origen de la instalación: VS 17.8.34316.72
Versión del manifiesto: 17.0.8478/8.0.100
Ruta de acceso del manifiesto: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.0.8478\WorkloadManifest.json
Tipo de instalación: Msi
Host:
Version: 8.0.0
Architecture: x64
Commit: 5535e31a71
.NET SDKs installed:
6.0.403 [C:\Program Files\dotnet\sdk]
8.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
C:\Users\ggomarighetti\source\repos\minerva-front\global.json
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
The text was updated successfully, but these errors were encountered: