I'm experiencing an exception on a Blazor Server chat application (with signalR) using my company's Azure Active Directory for authentication. First I successfully tested the chat code with no authentication. I then successfully tested the AAD settings in appsettings.json by running the default Blazor code with our test AAD. My environment is testing locally from my machine while VPN'd into company network.
Per the tutorial https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-tutorial-build-blazor-server-chat-app I added a call to AddAzureSingalR()
in Startup.ConfigureServices()
, and added the configuration to turn on Azure SignalR Service in appsettings.json for local development.
"Azure": {
"SignalR": {
"Enabled": true,
"ConnectionString": <your-connection-string>
}
}
Running the code produces the following exception in Program.cs --
Program.cs:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
Startup.cs:
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
services.AddSignalR().AddAzureSignalR();
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();
services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapHub<ChatHub>("/chathub");
});
}
The appsettings.json:
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "university.edu",
"TenantId": "xxx",
"ClientId": "xxxx",
"CallbackPath": "/signin-oidc"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Azure": {
"SignalR": {
"Enabled": true,
"ConnectionString": "Connection"
}
},
"AllowedHosts": "*"
}
Secrets.json:
{
"Azure:SignalR:ConnectionString": "Connection"
}
Connected services - Microsoft Identity and SignalR are ignored presently as they are configured in code: