Skip to content

SignalR .NET client: make auth refresh work behind a redirecting server (Azure SignalR) - #67612

Merged
BrennanConroy merged 3 commits into
dotnet:mainfrom
MoChilia:shiyingchen/auth_refresh_azure_signalr
Jul 20, 2026
Merged

BrennanConroy merged 3 commits into
dotnet:mainfrom
MoChilia:shiyingchen/auth_refresh_azure_signalr

Conversation

@MoChilia

@MoChilia MoChilia commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

SignalR .NET client: make auth refresh work behind a redirecting server (Azure SignalR)

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Summary of the changes (Less than 80 chars)

Makes the SignalR /refresh flow 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:

  • Negotiate redirects: the app server authenticates the app token and reports tokenLifetimeSeconds; the service returns the service token the transport uses.
  • /refresh must 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

  1. Send /refresh with the app token. Added GetRefreshRequestTokenAsync(), used by AccessTokenHttpMessageHandler for refresh requests. The app provider is captured into a new _appAccessTokenProvider field before transports overwrite HttpConnectionOptions.AccessTokenProvider with the service token.

  2. Adopt the refreshed token from the /refresh response. ParseRefreshResponse now reads an optional accessToken alongside tokenLifetimeSeconds; when present, it becomes the new transport credential (UpdateCachedToken), so Long Polling / SSE / reconnect keep working past the original token's expiry. Self-hosted omits accessToken, so this is a no-op there.

  3. Preserve tokenLifetimeSeconds across the redirect. Only overwrite _initialTokenLifetime when 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.

@MoChilia
MoChilia requested a review from BrennanConroy as a code owner July 6, 2026 03:26
Copilot AI review requested due to automatic review settings July 6, 2026 03:26
@MoChilia
MoChilia requested a review from halter73 as a code owner July 6, 2026 03:26
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jul 6, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @MoChilia. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 /refresh requests, even after negotiate redirects.
  • Parse an optional accessToken from the /refresh response and adopt it as the transport credential.
  • Preserve tokenLifetimeSeconds across 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.

@MoChilia

MoChilia commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

@MoChilia please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@dotnet-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"

Contributor License Agreement

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ASRS not returning a token lifetime?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine for now. ASRS might want to consider returning the token lifetime in its final negotiate response though.

@BrennanConroy
BrennanConroy merged commit 326d566 into dotnet:main Jul 20, 2026
24 checks passed
@gfoidl gfoidl added the area-signalr Includes: SignalR clients and servers label Jul 21, 2026
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-signalr Includes: SignalR clients and servers community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants