使用 identiyserver4 授权信号器核心集线器
Authorize signalr core hub using identiyserver4
我正在使用 visual studio 2019 预览版 Angular / .net core API 具有个人授权的后端模板。
我相信在此模板中使用了 identityserver4。
在 API 中有一个我正在尝试授权的信号器核心集线器。我在集线器上有授权属性。我还在 angular 信号器客户端 URL 查询字符串中指定令牌。
尽管如此,授权属性没有任何作用,无论有没有令牌,我都可以访问集线器。
JS / angular 客户端
ngOnInit() {
console.log(this.authService.getAccessToken().toPromise())
this._hubConnection = new signalR.HubConnectionBuilder()
//.withUrl('/handoverhub', {accessTokenFactory: () => this.token})
.withUrl('/handoverhub', { accessTokenFactory: () => {
return this.authService.getAccessToken().toPromise();
} })
.configureLogging(signalR.LogLevel.Information)
.build();
ASPNETCore代码
集线器使用 Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using HomecareShared.Models;
using HomecareShared.Models.DTOs;
using HomecareShared.Models.Handover;
using HomecareShared.Models.Notify;
using HomecareShared.Models.SharedResources;
using HomecareHandover.Repo;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.VisualBasic.ApplicationServices;
namespace HomecareHandover.Hubs {
[Authorize]
public class HandoverHub : Hub
启动的一些片段
app.UseAuthentication();
app.UseAuthorization();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<HandoverHub>("/handoverhub"); //For handover
endpoints.MapHub<TaskHub>("/taskhub"); //For task
});
app.UseIdentityServer();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddSignalR();
没有错误信息。我可以直接进入中心没问题。
我遇到过类似的问题,但 AzureSignalR
。我通过实施下面的代码解决了这个问题。您还应该在 UseEndpoints
;
之前调用 UseIdentityServer
app.UseAuthentication();
app.UseAuthorization();
app.UseAzureSignalR(routes =>
{
routes.MapHub<ChatHub>("/hubs/chat");
routes.MapHub<NotificationHub>("/hubs/notifications");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapHealthChecks("/healthz", new HealthCheckOptions() { });
});
顺便说一下,另一个关于 AzureSignalR 和纯 JWT 中的 Hub 授权的例子,但我放在这里是为了让你看一看
https://github.com/ilkerkaran/MySignalRPlayGround/blob/master/SignalRServer.API/Startup.cs
已修复!!!!
原来它是在 Startup.cs 文件中排序的。
我首先实现了 ilkerkaran 关于在 UseEndpoints 之前调用 identityserver 的建议。然后又过了 4 个小时,我将 app.UseAuthorization() 移动到 app.UseIdentityServer 下面并修复了它。
希望这对其他人有帮助。
我正在使用 visual studio 2019 预览版 Angular / .net core API 具有个人授权的后端模板。
我相信在此模板中使用了 identityserver4。
在 API 中有一个我正在尝试授权的信号器核心集线器。我在集线器上有授权属性。我还在 angular 信号器客户端 URL 查询字符串中指定令牌。
尽管如此,授权属性没有任何作用,无论有没有令牌,我都可以访问集线器。
JS / angular 客户端
ngOnInit() {
console.log(this.authService.getAccessToken().toPromise())
this._hubConnection = new signalR.HubConnectionBuilder()
//.withUrl('/handoverhub', {accessTokenFactory: () => this.token})
.withUrl('/handoverhub', { accessTokenFactory: () => {
return this.authService.getAccessToken().toPromise();
} })
.configureLogging(signalR.LogLevel.Information)
.build();
ASPNETCore代码 集线器使用 Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using HomecareShared.Models;
using HomecareShared.Models.DTOs;
using HomecareShared.Models.Handover;
using HomecareShared.Models.Notify;
using HomecareShared.Models.SharedResources;
using HomecareHandover.Repo;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.VisualBasic.ApplicationServices;
namespace HomecareHandover.Hubs {
[Authorize]
public class HandoverHub : Hub
启动的一些片段
app.UseAuthentication();
app.UseAuthorization();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<HandoverHub>("/handoverhub"); //For handover
endpoints.MapHub<TaskHub>("/taskhub"); //For task
});
app.UseIdentityServer();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddSignalR();
没有错误信息。我可以直接进入中心没问题。
我遇到过类似的问题,但 AzureSignalR
。我通过实施下面的代码解决了这个问题。您还应该在 UseEndpoints
;
UseIdentityServer
app.UseAuthentication();
app.UseAuthorization();
app.UseAzureSignalR(routes =>
{
routes.MapHub<ChatHub>("/hubs/chat");
routes.MapHub<NotificationHub>("/hubs/notifications");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapHealthChecks("/healthz", new HealthCheckOptions() { });
});
顺便说一下,另一个关于 AzureSignalR 和纯 JWT 中的 Hub 授权的例子,但我放在这里是为了让你看一看 https://github.com/ilkerkaran/MySignalRPlayGround/blob/master/SignalRServer.API/Startup.cs
已修复!!!!
原来它是在 Startup.cs 文件中排序的。
我首先实现了 ilkerkaran 关于在 UseEndpoints 之前调用 identityserver 的建议。然后又过了 4 个小时,我将 app.UseAuthorization() 移动到 app.UseIdentityServer 下面并修复了它。
希望这对其他人有帮助。