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

  1. Guard the call: `if (hub.state === HubConnectionState.Connected) await hub.refreshAuthentication();`.
  2. Use the built-in `withAuthenticationRefresh()` builder method which schedules refreshes only on an active connection.
  3. 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

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

Related errors


AI-assisted analysis of dotnet/aspnetcore@294cab2f9b (2026-08-06). Data as JSON: /api/errors/e534a6921eaf09db. Report an issue: GitHub.