dotnet/aspnetcore · error · Error
Cannot refresh authentication when the connection is not act
Error message
Cannot refresh authentication when the connection is not active.
What it means
Thrown at HubConnection.ts:620 at the top of `refreshAuthentication()` when the connection state is anything other than Connected. Refreshing authentication requires an active connection to POST the refresh request through; a Disconnected/Connecting/Reconnecting/Disconnecting connection has no valid refresh endpoint.
Source
Thrown at src/SignalR/clients/ts/signalr/src/HubConnection.ts:620
}
/** Registers a handler that will be invoked when the connection successfully reconnects.
*
* @param {Function} callback The handler that will be invoked when the connection successfully reconnects.
*/
public onreconnected(callback: (connectionId?: string) => void): void {
if (callback) {
this._reconnectedCallbacks.push(callback);
}
}
/** Refreshes the authentication state for this connection.
*
* @returns A Promise that resolves with the new server-reported token lifetime in seconds, or undefined when the server does not report one.
*/
public async refreshAuthentication(): Promise<number | undefined> {
if (this._connectionState !== HubConnectionState.Connected) {
throw new Error("Cannot refresh authentication when the connection is not active.");
}
const authenticationRefreshFeature = this.connection.features.authenticationRefresh as IAuthenticationRefreshFeature | undefined;
if (!authenticationRefreshFeature) {
throw new Error("Authentication refresh is only supported with HTTP-based connections.");
}
let newTokenLifetimeInSeconds: number | undefined;
try {
newTokenLifetimeInSeconds = await authenticationRefreshFeature.refreshAuthentication();
} catch (e) {
await this._invokeAuthenticationRefreshFailed(e);
throw e;
}
if (this._connectionState === HubConnectionState.Connected &&
this.connection.features.authenticationRefresh === authenticationRefreshFeature &&
this._isAutoAuthenticationRefreshEnabled() &&View on GitHub (pinned to 294cab2f9b)
Solutions
- Guard the call: `if (hub.state === HubConnectionState.Connected) await hub.refreshAuthentication();`.
- Use the built-in `withAuthenticationRefresh()` builder method which schedules refreshes only on an active connection.
- On a Disconnected connection, start() (or re-start) to get a fresh token via the access token factory instead of refresh.
Example fix
// before
await hub.refreshAuthentication(); // throws if not connected
// after
if (hub.state === HubConnectionState.Connected) {
await hub.refreshAuthentication();
} else {
await hub.start();
} Defensive patterns
Strategy: validation
Validate before calling
function refreshIfActive(hub) {
if (hub.state !== HubConnectionState.Connected) {
throw new Error(`Connection not active (state=${hub.state}); refresh ignored`);
}
return hub.refreshAuthentication();
} Type guard
function isConnectionActive(hub: HubConnection): boolean {
return hub.state === HubConnectionState.Connected;
} Try / catch
try { await hub.refreshAuthentication(); }
catch (e) {
if (/connection is not active/.test(String(e))) {
await hub.start();
return;
}
throw e;
} Prevention
- Guard refreshAuthentication calls with a state check.
- Cancel manual refresh timers in onclose.
- Prefer the builder's withAuthenticationRefresh auto-scheduling.
When it happens
Trigger: Calling `hub.refreshAuthentication()` when state is Disconnected (before start or after close), Connecting (during initial handshake), Reconnecting, or Disconnecting. The guard at line 619 fails immediately.
Common situations: Auto-refresh timer firing after the connection dropped; calling refresh from a UI button without checking state; refresh triggered in onclose before state fully transitions; reconnect loop calling refresh before the new connection is up.
Understand the failure class
- Authentication and authorization failures — expired tokens, bad credentials, and missing scopes.
Related errors
- Cannot refresh authentication before the connection is start
- The HubConnection must be in the Disconnected or Reconnectin
- Authentication refreshBeforeExpirationInMilliseconds must be
- Unexpected status code returned from authentication refresh
- Invalid authentication refresh response received: expected J
AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06).
Data as JSON: /api/errors/e534a6921eaf09db.
Report an issue: GitHub.