I'm working on a .NET 5 project implementing SignalR for use with an Angular 11 front-end, with OAuth2 authentication. The app is hosted on Kubernetes so I need to use skipNegotiation: true
and transport: HttpTransportType.WebSockets
to force the connection to establish itself on a single server behind my load balancer. This forces the SignalR library to send my access token as a query string parameter. After implementing I was getting a 404 response and it's taken me ages just to work out why: my access token is too long. If I send random characters as the access token up to a length of around 2035 characters, it works (well, it fails authentication obviously, but that's expected). If I send my full access token which is somewhere around 2500 characters long, it returns the 404
response.
I believe my implementation is fairly typical as it's just following example code so far:
Startup.cs:
// In ConfigureServices()
services.AddSignalR();
//In Configure()
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
endpoints.MapHub<MessageHub>("/message-hub");
});
MessageHub.cs:
public class MessageHub : Hub {
public async Task EchoTest(string message) => await Clients.All.SendAsync("echo", message);
public override Task OnConnectedAsync() {
Debug.WriteLine("connected");
return base.OnConnectedAsync();
}
}
Client:
const connection = new HubConnectionBuilder()
.withUrl('http://localhost:5001/message-hub', {
skipNegotiation: true,
transport: HttpTransportType.WebSockets,
accessTokenFactory: () => {
return 'MY_ACCESS_TOKEN'; // If 2036 characters or greater, 404 responses ensue
}
})
.build()
.start()
.then(() => console.log('Connection started'))
.catch(err => console.log('Error while starting connection: ' + err))
It's feasible to for me to reduce the length of the access token with a lot of refactoring of claim identifiers and etc. but that's a last resort. What I'm ideally after is a way to provide my current large access token as-is to SignalR without it breaking. Any suggestions would be appreciated.