I'm beginner with SignalR Hub and I've this problem.
When "OnConnectedAsync" is called the object Context.User is always empty.
This is the method:
[Authorize]
public override async Task OnConnectedAsync()
{
if(Context.User.Identity.IsAuthenticated)
{
await base.OnConnectedAsync();
Errore er = _g.AggiungiConnessione(Context.User.Identity.Name, Context);
}
}
the authentication is configurated like this:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var path = context.HttpContext.Request.Path;
if (!path.StartsWithSegments("/chatHub"))
return Task.CompletedTask;
var accessToken = context.Request.Headers[HeaderNames.Authorization];
if (!string.IsNullOrWhiteSpace(accessToken) && context.Scheme.Name ==
JwtBearerDefaults.AuthenticationScheme)
{
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
the HubConnection is declared like this:
if (user.Identity.IsAuthenticated)
{
hubConnection = new
HubConnectionBuilder().WithAutomaticReconnect().WithUrl(navmanager.ToAbsoluteUri("/chatHub"),
(opts) =>
{
opts.HttpMessageHandlerFactory = (message) =>
{
if (message is HttpClientHandler clientHandler)
{
clientHandler.ServerCertificateCustomValidationCallback += (sender,
certificate, chain, sslPolicyErrors) => { return true; };
}
return message;
};
opts.AccessTokenProvider = async () => await Task.FromResult(user.Identity.Name);
}
}
any suggestions?