为了账号安全,请及时绑定邮箱和手机立即绑定

使用 identiyserver4 授权 signalr core hub

使用 identiyserver4 授权 signalr core hub

C#
慕桂英3389331 2023-09-24 10:55:16
我正在使用具有单独授权的 Visual Studio 2019 预览版 Angular / .net core API 后端模板。我相信在这个模板中,正在使用identityserver4。在 API 中,有一个我正在尝试授权的信号器核心中心。我在集线器上有授权属性。我还在 Angular Signalr 客户端 URL 查询字符串中指定了令牌。尽管存在上述情况,授权属性没有任何作用,我可以使用或不使用令牌访问集线器。JS / 角度客户端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();使用 Microsoft.AspNetCore.SignalR 的 ASPNETCore 代码中心;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();没有错误消息。我可以直接进入集线器,没有问题。
查看完整描述

2 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

我也遇到过类似的问题,但是使用AzureSignalR. 我通过实现下面的代码解决了这个问题。您还应该UseIdentityServer先打电话UseEndpoints;


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 中的集线器授权的示例,但我放在这里,以便您可以查看 https://github.com/ilkerkaran/MySignalRPlayGround/blob/master/SignalRServer.API/Startup.cs


查看完整回答
反对 回复 2023-09-24
?
慕仙森

TA贡献1827条经验 获得超7个赞

固定的!!!!

原来它是在 Startup.cs 文件中排序的。

我首先实现了ilkerkaran关于在UseEndpoints之前调用identityserver的建议。然后又过了 4 个小时,我将 app.UseAuthorization() 移到了 app.UseIdentityServer 下面并修复了它。

希望这对其他人有帮助。


查看完整回答
反对 回复 2023-09-24
  • 2 回答
  • 0 关注
  • 65 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信