【发布时间】: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