Reputation: 174
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:
Upvotes: 1
Views: 275
Reputation: 174
Closing as I learned the connectionString is only relevant when application is deployed to Azure, which it is not in this case. I haven't published it anywhere yet. Revising question..
Upvotes: 0
Reputation: 10871
Please try by passing the connection string in start up in this way and execute
Ex:
services.AddSignalR().AddAzureSignalR(Configuration["Azure:SignalR:ConnectionString"]);
And make sure connection string is proper and correct without spaces in
secrets.json
{
"Azure:SignalR:ConnectionString": "Connection"
}
If above doesn’t resolve the issue configure the Azure connection string in the application by also adding app.UseAzureSignalR
in Startup.cs.
If you are making use of environment variables see this so reference which says
EnvironmentVariablesConfigurationProvider automatically replaces __ with : . So when you configures connection string via environment variables, you should use Azure__SignalR__ConnectionString as the key. When you configures it via JSON file, you should use the origin key Azure:SignalR:ConnectionString.
So when you try to configures connection string through environment variables, you should use Azure__SignalR__ConnectionString
as the key and if its via JSON file, you should use the origin key Azure:SignalR:ConnectionString
.
Upvotes: 1