【发布时间】: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