【问题标题】:SignalR with Azure AD (Blazor Server chat app)SignalR 与 Azure AD(Blazor 服务器聊天应用程序)
【发布时间】: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 请求。

标签: azure blazor


【解决方案1】:

不确定这是否会有所帮助。这就是我将 WebAssembly 主机(服务器)与 SignalR 集线器连接起来的方式。

services.TryAddEnumerable(
    ServiceDescriptor.Singleton<IPostConfigureOptions<JwtBearerOptions>,
    ConfigureJwtBearerOptions>());
public class ConfigureJwtBearerOptions : IPostConfigureOptions<JwtBearerOptions>
{
    public void PostConfigure(string name, JwtBearerOptions options)
    {
        var originalOnMessageReceived = options.Events.OnMessageReceived;
        options.Events.OnMessageReceived = async context =>
        {
            await originalOnMessageReceived(context);

            if (string.IsNullOrEmpty(context.Token))
            {
                var accessToken = context.Request.Query["access_token"];
                var requestPath = context.HttpContext.Request.Path;
                var endPoint = $"/chathub";

                if (!string.IsNullOrEmpty(accessToken) &&
                    requestPath.StartsWithSegments(endPoint))
                {
                    context.Token = accessToken;
                }
            }
        };
    }
}

【讨论】:

    【解决方案2】:

    没有人提供适用于 Blazor Server、SignalR 和 Azure AD 的简单聊天示例应用程序?来吧伙计们..这个平台上有很多专业人士。有没有有点人性的人提供一个项目示例而不将此请求标记为非投诉?

    将不胜感激。我已经尝试了从这个平台找到的所有东西,但都没有成功。

    【讨论】:

      猜你喜欢
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 2012-02-12
      相关资源
      最近更新 更多