https://learn.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,但没有解决方案)
//Program.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": "*"
}
2条答案
按热度按时间qmb5sa221#
不确定这是否有帮助。这是我如何用SignalR集线器连接WebAssembly主机(服务器)的。
gwbalxhn2#
我在这里发布了一个解决方案:Microsoft Learn
基本上:
1.手动获取主机.cshtml页面中的Cookie
1.将Cookie集合传递给app.razor,以便创建级联参数
1.示例化SignalR客户端时检索参数并手动填充Cookie容器(您可能在Web上看到过此代码,但如果没有步骤1和2,它将无法在Azure上工作,只能在IIS Express中工作)
Program.cs
_主机.cshtml:
app.razor:
Index.razor:
ChatHub.cs: