【发布时间】:2022-06-30 05:44:22
【问题描述】:
https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-tutorial-build-blazor-server-chat-app
如何在激活 Azure AD 的情况下使用它?当我在 Visual Studio 中本地运行时,它运行良好,但部署时它不适用于 Azure AD,只有在我删除 Azure AD 时它才能工作。
这是部署时和单击“聊天!”按钮后的错误消息。用户名文本框旁边:
“错误:启动聊天客户端失败:响应状态码不表示成功:403(禁止)。”
(我找到了类似Blazor Server SignalR Chat works on Local, not on Azure的其他线程,但没有解决方案)
//程序.cs
using BlazorApp6ADChat;
using BlazorApp6ADChat.Data;
using BlazorChat;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor()
.AddMicrosoftIdentityConsentHandler();
builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
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.UseStaticFiles();
app.UseRouting();
app.MapControllers();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.MapHub<BlazorChatSampleHub>(BlazorChatSampleHub.HubUrl);
app.UseAuthentication();
app.UseAuthorization();
app.Run();
//appsettings.json
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "xxx.onmicrosoft.com",
"TenantId": "xxx",
"ClientId": "xxx",
"CallbackPath": "/signin-oidc"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
【问题讨论】:
-
能否附上您在 appsettings.json 文件中的 Azure 配置(删除重要数据,如机密等)以及您在 startup.cs / program.cs 中的配置?也许有类似的错字。也许检查您在 Azure 服务器中设置的环境变量 (debug/development/prog) ;)
-
我已经添加了配置和代码,你发现有什么问题吗?它适用于调试时登录的 Azure AD/用户,Azure AD 可适用于我的应用程序中的所有其他内容,而不是 Microsoft 的 SignalRChat 示例。如果我删除 Azure AD,则聊天在部署时也能正常工作..
-
我可以发布我添加的中间件,让 WASM Signalr 与 Azure AD 一起工作。 services.TryAddEnumerable(ServiceDescriptor.Singleton
, ConfigureJwtBearerOptions>()); -
好吧,我会在大约 90 分钟后在某个 atm 的中间发布我的答案。对悬念感到抱歉。但是是的,ConfigureJwtBearerOptions 是我创建的一个类。
-
@WTech 请将我的答案标记为已接受。原因是 SignalR 不必使用 TCP,开箱即用的解决方案仅将令牌附加到 TCP 数据包。该中间件只是确保将令牌附加到 Hub 请求。