#4711·SignalR

AccessTokenFactory should be cacheable

Author: wilkinsonjCreated Jun 18, 2024Updated Sep 4, 2025

In the docs, it's recommended to refresh the accessToken within the AccessTokenFactory method. However, I have noticed that this method is called with every SignalR connection, regardless of the reason why that connection was required.

If this is an expensive (aka serverside) operation, it doesn't make sense to refresh the accessToken every time. Imagine a client with patchy internet having to reconnect, and that yielding an additional and unnecessary token refresh.

Instead, the accessToken should be cached until there is specifically a 401 exception, at which point it should be refreshed. At least there should be an option to do this.

Either that, or it should be possible to catch 401 exceptions specifically on the HubConnection, which could in turn be configured to trigger a token refresh. Right now, the exception is simply plaintext. Perhaps adding a status code property would be enough.