{"record":{"id":"e534a6921eaf09db","repo":"dotnet/aspnetcore","slug":"cannot-refresh-authentication-when-the-connection","errorCode":null,"errorMessage":"Cannot refresh authentication when the connection is not active.","messagePattern":"Cannot refresh authentication when the connection is not active\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/SignalR/clients/ts/signalr/src/HubConnection.ts","lineNumber":620,"sourceCode":"    }\n\n    /** Registers a handler that will be invoked when the connection successfully reconnects.\n     *\n     * @param {Function} callback The handler that will be invoked when the connection successfully reconnects.\n     */\n    public onreconnected(callback: (connectionId?: string) => void): void {\n        if (callback) {\n            this._reconnectedCallbacks.push(callback);\n        }\n    }\n\n    /** Refreshes the authentication state for this connection.\n     *\n     * @returns A Promise that resolves with the new server-reported token lifetime in seconds, or undefined when the server does not report one.\n     */\n    public async refreshAuthentication(): Promise<number | undefined> {\n        if (this._connectionState !== HubConnectionState.Connected) {\n            throw new Error(\"Cannot refresh authentication when the connection is not active.\");\n        }\n\n        const authenticationRefreshFeature = this.connection.features.authenticationRefresh as IAuthenticationRefreshFeature | undefined;\n        if (!authenticationRefreshFeature) {\n            throw new Error(\"Authentication refresh is only supported with HTTP-based connections.\");\n        }\n\n        let newTokenLifetimeInSeconds: number | undefined;\n        try {\n            newTokenLifetimeInSeconds = await authenticationRefreshFeature.refreshAuthentication();\n        } catch (e) {\n            await this._invokeAuthenticationRefreshFailed(e);\n            throw e;\n        }\n\n        if (this._connectionState === HubConnectionState.Connected &&\n            this.connection.features.authenticationRefresh === authenticationRefreshFeature &&\n            this._isAutoAuthenticationRefreshEnabled() &&","sourceCodeStart":602,"sourceCodeEnd":638,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/SignalR/clients/ts/signalr/src/HubConnection.ts#L602-L638","documentation":"Thrown by HubConnection.refreshAuthentication when `_connectionState` is not `Connected`. The refresh flow needs a live transport to round-trip a new token request; calling it before the connection is fully established, or after it has dropped, is rejected up front rather than producing a confusing transport error.","triggerScenarios":"Invoking `await hubConnection.refreshAuthentication()` while the connection is Disconnected, Reconnecting, or Disconnecting — e.g. immediately after start() has not yet resolved, or after an onclose handler has fired.","commonSituations":"Calling refreshAuthentication from a token-expiry handler that fires asynchronously after the connection has already dropped, or wiring it to a timer that races the connection lifecycle.","solutions":["Gate refreshAuthentication on the Connected state (check `hubConnection.state === 'Connected'`).","Drive the refresh from the library's automatic refresh scheduling rather than calling it manually.","Subscribe to onreconnected/onclose and disable manual refresh when not Connected."],"exampleFix":"// before\nawait hubConnection.refreshAuthentication(); // may throw if disconnected\n// after\nif (hubConnection.state === HubConnectionState.Connected) {\n  await hubConnection.refreshAuthentication();\n}","handlingStrategy":"validation","validationCode":"import { HubConnectionState } from \"@microsoft/signalr\";\nfunction canRefresh(hub: { state: HubConnectionState }): boolean {\n  return hub.state === HubConnectionState.Connected;\n}","typeGuard":"function isConnected(state: HubConnectionState): boolean {\n  return state === HubConnectionState.Connected;\n}","tryCatchPattern":null,"preventionTips":["Let the library schedule automatic refresh instead of calling manually.","Disable manual refresh timers in onclose.","Check hubConnection.state before invoking refreshAuthentication."],"tags":["authentication","state-machine","connection-lifecycle"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}