Repository navigation
SignalR .NET client: make auth refresh work behind a redirecting server (Azure SignalR) - #67612
Conversation
…fetime from an earlier negotiate before redirect
|
Thanks for your PR, @MoChilia. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
Pull request overview
This PR updates the SignalR .NET client authentication refresh flow to support negotiate redirects (notably Azure SignalR), where the app-plane token used for /refresh can differ from the service-plane transport token, and where /refresh may return an updated transport token.
Changes:
- Use the original (app-plane) token provider for
/refreshrequests, even after negotiate redirects. - Parse an optional
accessTokenfrom the/refreshresponse and adopt it as the transport credential. - Preserve
tokenLifetimeSecondsacross negotiate redirect hops when later hops omit it.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/AccessTokenHttpMessageHandler.cs | Uses app-plane token for refresh requests; adds a hook to update the handler’s cached token externally. |
| src/SignalR/clients/csharp/Http.Connections.Client/src/HttpConnection.cs | Captures app token provider, parses /refresh response for optional accessToken, and preserves initial token lifetime across redirect chain. |
| src/SignalR/clients/csharp/Client/test/UnitTests/HttpConnectionTests.AuthenticationRefresh.cs | Adds unit tests covering adopting accessToken from /refresh and using the app token after redirect. |
@dotnet-policy-service agree company="Microsoft" |
| } | ||
| _connectionToken = negotiationResponse.ConnectionToken; | ||
| _initialTokenLifetime = negotiationResponse.TokenLifetime; | ||
| // Preserve a token lifetime reported by an earlier negotiate in the redirect chain. |
There was a problem hiding this comment.
Is ASRS not returning a token lifetime?
There was a problem hiding this comment.
The application-side Azure SignalR SDK has included the token lifetime in the initial redirect negotiate response, and the client should preserve that value across the redirect chain. The Azure SignalR Service endpoint does not respond the token lifetime, as it is calculated by the application.
There was a problem hiding this comment.
I guess it's fine for now. ASRS might want to consider returning the token lifetime in its final negotiate response though.
SignalR .NET client: make auth refresh work behind a redirecting server (Azure SignalR)
Summary of the changes (Less than 80 chars)
Makes the SignalR
/refreshflow work when negotiate redirects to another server and the transport token differs from the app token (i.e. the Azure SignalR), without changing how self-hosted SignalR behaves. See Azure SignalR's refresh flow at Azure/azure-signalr#2282.Description
Why
Behind Azure SignalR:
tokenLifetimeSeconds; the service returns the service token the transport uses./refreshmust be sent with the app token, and its response returns a refreshed service token the transport must adopt.The stock client didn't handle this, so refresh sent the wrong token, ignored the refreshed one, and lost the advertised lifetime across the redirect.
Changes
Send
/refreshwith the app token. AddedGetRefreshRequestTokenAsync(), used byAccessTokenHttpMessageHandlerfor refresh requests. The app provider is captured into a new_appAccessTokenProviderfield before transports overwriteHttpConnectionOptions.AccessTokenProviderwith the service token.Adopt the refreshed token from the
/refreshresponse.ParseRefreshResponsenow reads an optionalaccessTokenalongsidetokenLifetimeSeconds; when present, it becomes the new transport credential (UpdateCachedToken), so Long Polling / SSE / reconnect keep working past the original token's expiry. Self-hosted omitsaccessToken, so this is a no-op there.Preserve
tokenLifetimeSecondsacross the redirect. Only overwrite_initialTokenLifetimewhen a negotiate hop actually reports one, so the value from the app-server hop isn't nulled out by the redirect hop, which is needed for the client's auto-refresh scheduler.