I created a Blazor server app in an effort to start learning Blazor and some newer technologies. I followed along with a hub chat tutorial that Microsoft has. Things worked great. I then added some basic cookie authentication and things were still on the up and up. Then I followed a tutorial from Carl Franklin on Blazor Train about connecting an app to Azure AD B2C. It works, except for the chat hub portion of the app. All of the other portions of the app work fine and show the user's info.
I'm almost positive that the "hubconnection"
is failing because it's not authenticating because it's not getting the access token. The failure happens on the await hubConnection.StartAsync();
line with the error System.Text.Json.JsonReaderException: '<' is an invalid start of a value
. I'm pretty sure that HTML is coming back with a 403 message or something like that.
So I guess I technically have 2 questions:
How can I view the
value
that's causing theHubConnectionBuilder
to error out? Even if I put in breakpoints I can never see what the value is that's causing it to choke.How can I pass the access token to the HubConnectionBuilder?
I've found lots of "ways" to do this that are either outdated or I couldn't make work:
Uses AddAzureADB2CBearer which is deprecated
Passes parameters to App which I can't get to work
This is what was working with Cookie auth:
hubConnection = new HubConnectionBuilder()
.WithUrl(
NavigationManager.ToAbsoluteUri("/chathub"),
config => config.UseDefaultCredentials = true)
.Build();
And now it seems like I need to pass in an access token based off of this Microsoft page about Auth and SignalR but
hubConnection = new HubConnectionBuilder()
.WithUrl(
NavigationManager.ToAbsoluteUri("/chathub"), options =>
{
options.AccessTokenProvider = () => Task.FromResult(_myAccessToken);
})
.Build();
Here is what I'm doing in Startup to get B2C working based on Carl Franklin YouTube video
//****Azure B2C****//
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAdB2C"));
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy
options.FallbackPolicy = options.DefaultPolicy;
});
services.AddRazorPages();
services.AddServerSideBlazor()
.AddMicrosoftIdentityConsentHandler();