.net 核心 2.1
中心代码:
[Authorize]
public class OnlineHub : Hub
{
public override async System.Threading.Tasks.Task OnConnectedAsync()
{
int userId = Context.UserIdentifier;
await base.OnConnectedAsync();
}
[AllowAnonymous]
public override async System.Threading.Tasks.Task OnDisconnectedAsync(Exception exception)
{
var b = Context.ConnectionId;
await base.OnDisconnectedAsync(exception);
}
客户端代码:
$(document).ready(() => {
let token = "token";
const connection = new signalR.HubConnectionBuilder()
.withUrl("https://localhost:44343/online", { accessTokenFactory: () => token })
.configureLogging(signalR.LogLevel.Debug)
.build();
connection.start().catch(err => console.error(err.toString()));
});
没有 [Authorize] 一切正常,除了 OnConnectedAsync 中的 Context.UserIdentifier,这是可以解释的,但是......在 Hub 类上使用 [Authorize] 属性,OnConnectedAsync 开始工作并且 OnDisconnected 根本不触发,包括 30 秒超时(通过默认)。
有什么想法吗?
最佳答案
如果您附加了调试器并通过关闭浏览器选项卡关闭了客户端,那么您永远不会观察到 OnDisconnectedAsync
被触发。这是因为 SignalR 检查是否附加了调试器并且不触发某些超时以使调试更容易。如果您通过在客户端上调用 stop 来关闭,那么您应该看到调用了 OnDisconnectedAsync
。
关于asp.net-core - ASP NET Core SignalR OnDisconnectedAsync 未使用集线器触发 [授权],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61713252/