使用参考令牌的 ASPNETCore SignalR 身份验证
ASPNETCore SignalR authentication with Reference token
我们在 Web API (netcoreapp2.2) 中使用 ASPNETCore.SignalR 1.1.0。
身份验证:我们正在为我们的项目使用 IdentityServer4 身份验证。
Startup.cs
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://IdentityServerDomainURL:8081/";
options.RequireHttpsMetadata = false;
options.ApiName = "name";
options.ApiSecret = "secret";
});
在 WebAPI 应用程序中,我们添加了 SignalR Hub。
JavaScript 客户端连接到此集线器。
以下是连接hub的JS客户端代码
var connection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:52177/SignalRHub/",
{
accessTokenFactory: () => "referencetokenValue"
}).build();
JS 客户端在连接到 Hub 时正在传递引用令牌。
我们需要在 WebAPI 项目中对 SignalR 使用 Reference token 认证。
在 Microsoft 的站点中,仅提供了 SignalR 的 JWT 令牌身份验证文档。未在任何地方找到有关参考令牌的任何文档。
需要帮助在 startup.cs 文件中添加参考令牌身份验证的配置。
找到解决方案 here。
查询字符串中的任何标记都可以添加到请求中 headers,如下所示:-
app.Use(async (context, next) =>
{
if (context.Request.Path.Value.StartsWith("/SignalRHub/"))
{
var bearerToken = context.Request.Query["access_token"].ToString();
if (!String.IsNullOrEmpty(bearerToken))
context.Request.Headers.Add("Authorization", new string[] { "bearer " + bearerToken });
}
await next();
});
以上代码需要在启动的Configure函数中添加class。
我们在 Web API (netcoreapp2.2) 中使用 ASPNETCore.SignalR 1.1.0。
身份验证:我们正在为我们的项目使用 IdentityServer4 身份验证。
Startup.cs
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://IdentityServerDomainURL:8081/";
options.RequireHttpsMetadata = false;
options.ApiName = "name";
options.ApiSecret = "secret";
});
在 WebAPI 应用程序中,我们添加了 SignalR Hub。 JavaScript 客户端连接到此集线器。 以下是连接hub的JS客户端代码
var connection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:52177/SignalRHub/",
{
accessTokenFactory: () => "referencetokenValue"
}).build();
JS 客户端在连接到 Hub 时正在传递引用令牌。 我们需要在 WebAPI 项目中对 SignalR 使用 Reference token 认证。
在 Microsoft 的站点中,仅提供了 SignalR 的 JWT 令牌身份验证文档。未在任何地方找到有关参考令牌的任何文档。
需要帮助在 startup.cs 文件中添加参考令牌身份验证的配置。
找到解决方案 here。
查询字符串中的任何标记都可以添加到请求中 headers,如下所示:-
app.Use(async (context, next) =>
{
if (context.Request.Path.Value.StartsWith("/SignalRHub/"))
{
var bearerToken = context.Request.Query["access_token"].ToString();
if (!String.IsNullOrEmpty(bearerToken))
context.Request.Headers.Add("Authorization", new string[] { "bearer " + bearerToken });
}
await next();
});
以上代码需要在启动的Configure函数中添加class。