dapr / dapr/dotnet-sdk

Dapr authentication not working alongside Aspire and KeyCloak

Open
#1,493 3 comments 0 reactions 1 assignee Claimed by @WhitWaldo View on GitHub
area/client
Dominant language
C#
Stars
1.2k
Forks
378
Avg merge
2d 1h
Merged PRs (30d)
6

Description

As reported on Discord [here](https://discord.com/channels/778680217417809931/778680217417809934/1352554821290426459):

> I have and Aspire.Net 9.1 Project running under .net 9 Combined with the keycloak preview package for aspire. The Website and the service are using authentication with keycloak and are working. My problem is as soon as I am using Dapr authentication fails. It looks like the Dapr sidecars do not transport the token and I found absolutely no documentation of how to set this up in an Aspire environment.

This is the [preview Keycloak package](https://www.nuget.org/packages/Aspire.Keycloak.Authentication/9.1.0-preview.1.25121.10?_src=template) in question. It adds JwtBearer and OpenId Connect authentication to the project via a Keycloak.

The reporting user got two errors when calling the service with Dapr:
> 2025-03-21T09:17:46 fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
2025-03-21T09:17:46 An unhandled exception has occurred while executing the request.
2025-03-21T09:17:46 Dapr.Client.InvocationException: An exception occurred while invoking method: 'weatherforecast' on app-id: 'weatherservice'
2025-03-21T09:17:46 ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).

and

> 2025-03-21T09:17:46 fail: Microsoft.AspNetCore.Server.Kestrel[13]
2025-03-21T09:17:46 Connection id "0HNB8A3D98IJ4", Request id "0HNB8A3D98IJ4:00000011": An unhandled exception was thrown by the application.
2025-03-21T09:17:46 Dapr.Client.InvocationException: An exception occurred while invoking method: 'weatherforecast' on app-id: 'weatherservice'
2025-03-21T09:17:46 ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized).

As the reporter continued:
> To me it looks like that the Autorization Handler is not added to the call or not delivered.
For the HTTPClient I could call:

```cs
builder.Services.AddHttpClient(client =>
{
client.BaseAddress = new("https+http://weatherservice");
})
.AddHttpMessageHandler();
```

But they found no counterpart for Dapr, not an example using Authentication inside an Aspire project.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.