signalr_client
signalr_client copied to clipboard
Can not connect to a hub that has Authorize property
trafficstars
my dontet project has a hub that requires users to have a valid jwt token, my configuration is
HubConnectionBuilder()
.withUrl(
"$_serverUrl/hubName",
options: HttpConnectionOptions(
accessTokenFactory: ()=> "",
logger: Logger("SignalR - transport"),
requestTimeout: 50000,
logMessageContent: true,
)),
)
.withHubProtocol(MessagePackHubProtocol())
.withAutomaticReconnect()
.configureLogging(Logger("SignalR - hub"))
.build().connect();
but I am getting the following error.
[log] FINER: 2022-11-16 22:27:07.957328: Selecting transport 'HttpTransportType.WebSockets'.
[log] FINEST: 2022-11-16 22:27:07.957909: (WebSockets transport) Connecting
[log] FINEST: 2022-11-16 22:27:07.958410: WebSocket try connecting to 'wss://653e-185-84-71-75.eu.ngrok.io/game-hub/63752f65ed1cf3b745c01953?id=59-ARneXOIDjkQTO_gUZqg&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MzcxMmRiZGU4MjUxZjVkMDJjZTc2YjMiLCJqdGkiOiJaTkp2U1c0N0JyX1hNS29seGxXdDciLCJpYXQiOjE2Njg2Mzg2MjEsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjgwMDEiLCJhdWQiOiJodHRwczovL2xvY2FsaG9zdDo0MjAwIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiYWRtaW4iLCJuYmYiOjE2Njg1NTIyMjEsImV4cCI6MTY2ODYzODYyMX0.WcsLGD_8nF9Yqa0IJOuBAqCUShqCVPC2949G3R359T8'.
[log] INFO: 2022-11-16 22:27:07.959286: WebSocket connected to 'wss://653e-185-84-71-75.eu.ngrok.io/game-hub/63752f65ed1cf3b745c01953?id=59-ARneXOIDjkQTO_gUZqg&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MzcxMmRiZGU4MjUxZjVkMDJjZTc2YjMiLCJqdGkiOiJaTkp2U1c0N0JyX1hNS29seGxXdDciLCJpYXQiOjE2Njg2Mzg2MjEsImlzcyI6Imh0dHBzOi8vbG9jYWxob3N0OjgwMDEiLCJhdWQiOiJodHRwczovL2xvY2FsaG9zdDo0MjAwIiwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjoiYWRtaW4iLCJuYmYiOjE2Njg1NTIyMjEsImV4cCI6MTY2ODYzODYyMX0.WcsLGD_8nF9Yqa0IJOuBAqCUShqCVPC2949G3R359T8'.
[log] FINER: 2022-11-16 22:27:07.959951: The HttpConnection connected successfully.
[log] FINER: 2022-11-16 22:27:07.960187: Sending handshake request.
[log] FINEST: 2022-11-16 22:27:07.960602: (WebSockets transport) sending data. String data of length 39. Content: '{"protocol":"messagepack","version":1}'.
[log] INFO: 2022-11-16 22:27:07.960941: Using HubProtocol 'messagepack'.
[log] FINER: 2022-11-16 22:27:08.429657: HttpConnection.stopConnection(Unknown) called while in state ConnectionState.Connected.
I am sure the connection is failing because of the Authorize Attribute, removing the attribute from the hub solves the problem, but I need the authorize tag.
i have same problem. please send me solution if you ressolve it.
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
{
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
Add jwt bearer event and map the access_token gotten from query to context, since Authorize attribute of signalr does not get data from http request like in a controller.