我在Blazor Server聊天应用程序(使用signalR)上遇到一个异常,它使用我公司的Azure Active Directory进行身份验证。首先,我在没有身份验证的情况下成功地测试了聊天代码。然后,我通过在我们的测试AAD中运行默认的Blazor代码,在appsettings.json中成功地测试了AAD设置。当VPN接入公司网络时,我的环境正在从我的机器进行本地测试。根据教程https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-tutorial-build-blazor-server-chat-app,我在Startup.ConfigureServices()中添加了一个对AddAzureSingalR()的调用,并添加了在appsettings.json中打开Azure SignalR服务以进行本地开发的配置。
"Azure": {
"SignalR": {
"Enabled": true,
"ConnectionString": <your-connection-string>
}
} 运行代码会在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");
});
}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"
}连接的服务-微软Identity和SignalR目前被忽略,因为它们是在代码中配置的:

发布于 2021-10-23 10:03:53
请尝试以这种方式传递启动中的连接字符串并执行
例如:
services.AddSignalR().AddAzureSignalR(Configuration["Azure:SignalR:ConnectionString"]);
并确保连接字符串正确无误,且
secrets.json
{
"Azure:SignalR:ConnectionString": "Connection"
}如果上面不能解决这个问题,可以通过在Startup.cs中添加app.UseAzureSignalR来配置应用程序中的Azure连接字符串。
如果您正在使用环境变量,请参阅this so reference,它写道
EnvironmentVariablesConfigurationProvider会自动将__替换为:。因此,当您通过环境变量配置连接字符串时,您应该使用Azure__SignalR__ConnectionString作为键。
因此,当您尝试通过环境变量配置连接字符串时,应使用Azure__SignalR__ConnectionString作为键,如果通过JSON文件,则应使用原始键Azure:SignalR:ConnectionString。
发布于 2021-10-29 19:07:59
据我所知,关闭connectionString只有在应用程序部署到Azure时才相关,而在本例中并不是这样。我还没有把它发布到任何地方。正在修改问题..
https://stackoverflow.com/questions/69682909
复制相似问题