部署至IIS托管服务器的Blazor内部WebAPI请求超时问题排查
问题:Blazor应用部署到IIS第三方服务器后调用WebAPI超时,无法获取Azure AD B2C OpenID配置
问题详情
- 本地开发环境及Azure部署环境下运行正常,部署至新安装的IIS第三方托管服务器后,Razor页面调用WebAPI接口(示例代码:
await Http.GetFromJsonAsync<PersData.Shared.UserInfo>("SecureNote");)出现超时 - 异常日志显示:无法从地址
https://xxxx.b2clogin.com/xxx.onmicrosoft.com/B2C_1_signinandregister/v2.0/.well-known/openid-configuration获取OpenID配置,因HttpClient 60秒超时导致请求取消 - 已更新相关依赖、调整响应等待逻辑,问题仍未解决,怀疑是服务器缺失相关配置
提供的Startup.cs代码
public void ConfigureServices(IServiceCollection services) { services.Configure<RazorPagesOptions>(options => options.RootDirectory = "/Pages"); services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>(); services.AddScoped<SignOutSessionStateManager>(); //if (!services.Any(x => x.ServiceType == typeof(HttpClient))) //{ // services.AddScoped<HttpClient>(s => // { // var urlHelper = s.GetRequiredService<NavigationManager>(); // return new HttpClient // { // BaseAddress = new Uri(urlHelper.BaseUri) // }; // }); //} services.AddMatBlazor(); services.Configure<Models.MailSettings>(Configuration.GetSection("MailSettings")); services.AddSwaggerGen(); services.AddTransient<IMailService, MailService>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAd")); services.AddControllersWithViews(); services.AddRazorPages(); services.AddLocalization(); // options => options.ResourcesPath = "Resource"); var supportedCultures = new List<CultureInfo> { new CultureInfo("de-CH"), new CultureInfo("en") }; services.Configure<RequestLocalizationOptions>(options => { options.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("de-CH"); options.SupportedCultures = supportedCultures; }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseWebAssemblyDebugging(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseBlazorFrameworkFiles(); app.UseStaticFiles(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"); }); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); var localizeOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); app.UseRequestLocalization(localizeOptions.Value); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapControllers(); endpoints.MapFallbackToPage("/_Host"); }); }
解决方案建议
- 检查服务器网络连通性:在IIS服务器上直接访问该OpenID配置地址,确认是否能正常返回内容。排查服务器防火墙、安全组是否阻止了对
b2clogin.com的HTTPS(443端口)请求 - 调整HttpClient超时配置:默认60秒超时可能无法满足受限网络环境需求,可通过配置延长超时时间。例如在注册HttpClient时设置:
services.AddScoped<HttpClient>(s => { var client = new HttpClient(); client.Timeout = TimeSpan.FromMinutes(2); return client; }); - 验证Azure AD B2C配置:核对appsettings.json中的
AzureAd配置项(TenantId、ClientId、Authority等)是否与Azure部署环境完全一致,避免因配置错误导致请求失败 - 检查服务器.NET环境:确保服务器安装了与应用匹配的.NET SDK/Runtime版本,且ASP.NET Core模块已正确安装并在IIS中注册
- 启用详细日志排查:在应用中开启更详细的HTTP请求日志,查看请求过程中的具体错误(如DNS解析失败、SSL证书验证异常等)
- 配置代理(若服务器在内网):如果服务器处于内网环境,需要为HttpClient配置代理服务器以访问外部资源,可通过添加HttpClientHandler实现:
services.AddScoped<HttpClient>(s => { var handler = new HttpClientHandler { Proxy = new WebProxy("http://your-proxy-address:port"), UseProxy = true }; return new HttpClient(handler); });
内容的提问来源于stack exchange,提问作者PScherrer
相关产品推荐
相关产品推荐

