【问题标题】:Authorize signalr core hub using identiyserver4使用 identiyserver4 授权信号器核心集线器
【发布时间】:2019-12-31 12:52:13
【问题描述】:

我正在使用 Visual Studio 2019 预览版 Angular / .net core API 后端模板,具有个人授权。

我相信在这个模板中,正在使用 identityserver4。

在 API 中有一个我正在尝试授权的信号器核心集线器。我在集线器上有 Authorize 属性。我还在 Angular Signalr 客户端 URL 查询字符串中指定了令牌。

尽管如此,authorize 属性无效,无论有没有令牌,我都可以访问集线器。

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

一些启动的sn-ps

        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();

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

【问题讨论】:

  • 您使用的是 Microsoft.AspNet.SignalR.AuthorizeAttribute 而不是 Microsoft.AspNetCore.Authorization.AuthorizeAttribute?
  • 两个都试过了,都不管用。

标签: c# asp.net-core signalr identityserver4


【解决方案1】:

我遇到了类似的问题,但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

【讨论】:

  • @Jamie 您是否有任何控制器来检查授权是否仅适用于集线器或根本不起作用?
  • 刚刚检查了控制器,它按预期工作 - 没有返回数据。 401 状态码。
【解决方案2】:

已修复!!!!

原来它是在 Startup.cs 文件中订购的。

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

希望这对其他人有所帮助。

【讨论】:

  • 请将@ilkerkaran 的答案标记为正确
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 2018-06-15
  • 1970-01-01
  • 2018-03-24
  • 1970-01-01
相关资源
最近更新 更多