“Websocket连接失败,当我向hub添加了Authorize属性时”

huangapple go评论75阅读模式
英文:

Websocket connection failed when I added Authorize attribute to the hub

问题

我在连接到 WebSocket 时遇到了一个奇怪的问题。当我在 hub 的顶部添加了 Authorize 属性时,我的应用程序不想连接到 wss。然而,当我尝试发送请求时,它仍然按预期工作(我猜它是通过 http 完成的?)。此外,当我检查我的网络标签时,它并没有显示 401 错误或任何内容,状态只是显示为已完成。我在这里做错了什么?

这是我收到的错误消息:
https://i.stack.imgur.com/BLyZM.png

这是我的网络标签的截图:
https://i.stack.imgur.com/b5c3v.png

这是我当前实现 hub 的方式:

using Duende.IdentityServer.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using System.Security.Claims;

namespace BagiBagiDev.Server.Hubs
{
    [Authorize]
    public class PaymentHub : Hub
    {
        public async Task TestMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
        public override async Task OnConnectedAsync()
        {
            var username = Context.User.FindFirst(c => c.Type == ClaimTypes.Name).Value;
            if (!string.IsNullOrEmpty(username))
            {
                await Groups.AddToGroupAsync(Context.ConnectionId, username);
            }
            await base.OnConnectedAsync();
        }
    }
}

我尝试在互联网上搜索,很多人都指向了一个 vscode 的 bug,但我认为这应该已经在我当前的版本(v 17.6)中修复了,所以我不确定发生了什么事情。

英文:

So I am encountering this weird behavior with my blazor app when connecting to websocket. When I added Authorize attribute at the top of the hub, my application doesn't want to connect to the wss. However, when I tried sending a request, it still works as expected (I am guessing its doing it over http?). Also, when I checked my network tab, it doesn't say 401 error or anything, the status of it just says finished. What did I do wrong here?

This is the error message I got
https://i.stack.imgur.com/BLyZM.png

And this is the screenshot of my network tab
https://i.stack.imgur.com/b5c3v.png

And this is how I currently implement my hub

using Duende.IdentityServer.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using System.Security.Claims;

namespace BagiBagiDev.Server.Hubs
{
    [Authorize]
    public class PaymentHub : Hub
    {
        public async Task TestMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
        public override async Task OnConnectedAsync()
        {
            var username = Context.User.FindFirst(c => c.Type == ClaimTypes.Name).Value;
            if (!string.IsNullOrEmpty(username))
            {
                await Groups.AddToGroupAsync(Context.ConnectionId, username);
            }
            await base.OnConnectedAsync();
        }
    }
}

I tried searching on the internet and a lot of people were pointing to a vscode bug, but I think that should've been fixed with the current version that I have (v 17.6) so I am not sure what's going on.

答案1

得分: 1

以下是翻译好的部分:

"All credits goes to this guy right here!"

"Turns out I miss a middleware to check for when the accessToken was received in the middleware."

"Now it works like a charm. Thanks again for all your help Brian!"

英文:

All credits goes to this guy right here!

Turns out I miss a middleware to check for when the accessToken was received in the middleware.

builder.Services.TryAddEnumerable(
            descriptor: ServiceDescriptor.Singleton<
                IPostConfigureOptions<JwtBearerOptions>,
                ConfigureJwtBearerOptions>());

Now it works like a charm. Thanks again for all your help Brian!

huangapple
  • 本文由 发表于 2023年6月13日 05:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定