使用 identiyserver4 授权 signalr core hub

我正在使用具有单独授权的 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();

没有错误消息。我可以直接进入集线器,没有问题。


慕桂英3389331
浏览 68回答 2
2回答

慕神8447489

我也遇到过类似的问题,但是使用AzureSignalR. 我通过实现下面的代码解决了这个问题。您还应该UseIdentityServer先打电话UseEndpoints;app.UseAuthentication();app.UseAuthorization();app.UseAzureSignalR(routes =>{&nbsp; &nbsp; routes.MapHub<ChatHub>("/hubs/chat");&nbsp; &nbsp; routes.MapHub<NotificationHub>("/hubs/notifications");});app.UseEndpoints(endpoints =>{&nbsp; &nbsp; endpoints.MapDefaultControllerRoute();&nbsp; &nbsp; endpoints.MapHealthChecks("/healthz", new HealthCheckOptions() { });});顺便说一句,另一个关于 AzureSignalR 和纯 JWT 中的集线器授权的示例,但我放在这里,以便您可以查看 https://github.com/ilkerkaran/MySignalRPlayGround/blob/master/SignalRServer.API/Startup.cs

慕仙森

固定的!!!!原来它是在 Startup.cs 文件中排序的。我首先实现了ilkerkaran关于在UseEndpoints之前调用identityserver的建议。然后又过了 4 个小时,我将 app.UseAuthorization() 移到了 app.UseIdentityServer 下面并修复了它。希望这对其他人有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP