I have a asp.net web api application with some controllers and a signalR hub. JWT tokens validation with Azure AD B2C is configured like this:
services.AddAuthentication(AzureADB2CDefaults.JwtBearerAuthenticationScheme)
.AddAzureADB2CBearer(options => _configuration.Bind("AzureAdB2C", options))
This works fine with controllers, and I don't have to worry about the intricacies of Azure AD B2C token validation.
Now, for the signalR hub to support Web Sockets or Server-sent events, the authentication token should be read from the querystring. I'm supposed to handle the OnMessageReceived event like this :
services.AddAuthentication(...)
.AddJwtBearer(options =>
{
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
// If the request is for our hub...
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) &&
(path.StartsWithSegments("/hubs/chat")))
{
// Read the token out of the query string
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
Unfortunately, the AzureAdB2COptions object does not give me access to the authentication events.
How can I reconcile both approaches ?