【问题标题】:Microsoft.Identity.Web and ASP.NET Core SignalR JWT authenticationMicrosoft.Identity.Web 和 ASP.NET Core SignalR JWT 身份验证
【发布时间】:2020-12-09 00:50:44
【问题描述】:

我正在使用 ASP.NET Core 制作一个 Web 应用程序,该应用程序也使用 SignalR Core 提供实时功能。我使用 Azure AD B2C 进行用户管理。我已成功使用 Microsoft.Identity.Web (https://github.com/AzureAD/microsoft-identity-web) 使用 Azure AD B2C 生成的令牌保护我的 API 端点。

我想为我的 SignalR Core 集线器做同样的事情。文档读取以向您的集线器/方法添加适当的注释,我已经这样做了。 SignalR 的客户端库将访问令牌添加为查询参数,必须在 ASP.NET 核心应用程序的配置中手动提取并添加到上下文中,就像这样。

    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer(options =>
    {
        options.Events = new JwtBearerEvents
        {
            OnMessageReceived = context =>
            {
                var accessToken = context.Request.Query["access_token"];

                // If the request is for our hub...
                var path = context.HttpContext.Request.Path;
                if (!string.IsNullOrEmpty(accessToken) &&
                    (path.StartsWithSegments("/hubs/chat")))
                {
                    // Read the token out of the query string
                    context.Token = accessToken;
                }
                return Task.CompletedTask;
            }
        };
    });

但是,这似乎与Microsoft.Identity.Web 提供的配置不兼容,这里:

        services
            .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApi(Configuration.GetSection("AzureAdB2C"));

如何使 SignalR 与 Microsoft.Identity.Web 一起工作?

【问题讨论】:

  • 杰西,我对你想要达到的目标完全感到困惑。这是绕过 Microsoft.Identity.Web。为什么需要访问令牌到达您的 Web API?您提到的文档有链接吗?如果您使用的是 MIcrosoft.Identity.Web,我想看看它(也许有更新?),只需在您的控制器操作中使用 IDownstreamWebApi、GraphServiceClient 或 ITokenAcquisition?

标签: azure asp.net-core signalr


【解决方案1】:

应该这样做:

services
    .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApi(configuration);

services.Configure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
    Func<MessageReceivedContext, Task> existingOnMessageReceivedHandler = options.Events.OnMessageReceived;
    options.Events.OnMessageReceived = async context =>
    {
      await existingOnMessageReceivedHandler(context);

      StringValues accessToken = context.Request.Query["access_token"];
      PathString path = context.HttpContext.Request.Path;

      // If the request is for our hub...
      if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
      {
        // Read the token out of the query string
        context.Token = accessToken;
      }
    };
});

您可以通过这种方式配置 JwtBearerOptions 对象,而不是添加 JwtBearer。

改编自本文档:https://github.com/AzureAD/microsoft-identity-web/wiki/customization

【讨论】:

    【解决方案2】:

    您可以使用Visual Studio设置SignalR连接,然后在startup.cs中添加这一行(VS可能会自动添加)

    services.AddSignalR().AddAzureSignalR();
    

    This dev sample 已设置 SignalR,只是缺少连接字符串,但可能会让您知道该怎么做。其中大部分是使用 VS 自动完成的。如果您在设置时遇到问题,请在repo 中打开一个问题。谢谢。

    【讨论】:

      猜你喜欢
      • 2019-05-25
      • 2019-09-06
      • 2021-04-20
      • 2018-09-02
      • 2018-01-28
      • 2018-03-25
      • 1970-01-01
      • 2021-10-27
      • 2021-12-17
      相关资源
      最近更新 更多