信号器用户从未显示经过身份验证

发布于 2025-01-13 08:51:02 字数 1887 浏览 1 评论 0原文

将最新的 SignalR (6.0.3) 添加到我的 .net 6 API 中。一切正常,直到我尝试获取用户身份 - 它总是未经授权。我真的只是想从令牌中获取索赔,所以也许我找错了地方。

在我的 SignalR hub 类中,有一个获取特定声明的简单方法:(代码很粗糙,只是试图让它工作)

public int GetPlayerId()
{
    int id = 0;
    try
    {
        var identity = (ClaimsIdentity)Context.User.Identity;
        id = Int32.Parse(identity.FindFirst("playerId").ToString());
    } catch (Exception ex)
    {
        return 0;
    }
    return id;
}

无论我做什么,Context.User 看起来都是这样的:

在此处输入图像描述

我甚至不知道该去哪里开始调试,因为授权在整个应用程序中按预期工作。这似乎指向 SignalR 问题,但我能找到的关于此问题的少数帖子大多严重过时,并且没有产生明显的影响。 根据文档,这应该“仅适用于”应用程序的现有授权

非常感谢您对检查内容或提供其他详细信息的任何见解。


编辑:附加信息

[Authorize]装饰器添加到我的集线器类本身似乎不起作用。无论如何,我都可以发送和接收。授权继续按其他地方的预期工作。

Added the latest SignalR (6.0.3) to my .net 6 API. Everything works there until I try to grab the user identity - it is always unauthorized. I'm really just trying to get a claim from the token, so maybe I'm looking in the wrong place.

  • The entire setup is localhost API supplying localhost Vue3 SPA.
  • Authorization works as intended for all API controller actions, following this tutorial.
  • SignalR Hub communicates as intended with front-end otherwise - receiving and sending.
  • The SignalR Hub app.MapHub<ChatHub>("/chat"); is the last line in Program.cs before app.Run();
  • I tried updating the connection from front-end to include accessTokenFactory function, and I can confirm token is correctly supplied here. However, this should not be necessary with cookie authentication.

In my SignalR hub class is a simple method for getting a particular claim: (the code is rough, just trying to get it to work)

public int GetPlayerId()
{
    int id = 0;
    try
    {
        var identity = (ClaimsIdentity)Context.User.Identity;
        id = Int32.Parse(identity.FindFirst("playerId").ToString());
    } catch (Exception ex)
    {
        return 0;
    }
    return id;
}

Context.User looks like this regardless of what I do:

enter image description here

I'm not sure where to even begin to debug, as authorization is working as intended across the entirety of the application otherwise. That would seem to point to a SignalR issue, but the few posts I could find about this were mostly severely outdated and made no discernable impact. According to the documentation, this should "just work" with the application's existing authorization.

Any insight on what to check into or additional details to provide is deeply appreciated.


Edit: Additional information

Adding the [Authorize] decorator to my hub class itself does not appear to work. I am able to send and receive regardless. Authorization continues to work as intended elsewhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2025-01-20 08:51:02

affore-linked 身份验证方案中的 JwtMiddleware 不会影响 SignalR 集线器的 Context 对象。

我不只是使用 accountId,而是使用经过验证的 JWT 令牌并向 HttpContext User 添加身份。这可能并不完美,但我希望它对将来的人有所帮助:

    var jwtToken = jwtUtils.ValidateJwtToken(token);
    if (jwtToken != null)
    {
        int? accountId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value);
        if (accountId != null)
        {
            // attach account to context on successful jwt validation
            context.Items["Account"] = await dataContext.Accounts.FindAsync(accountId);
            context.User.AddIdentity(new ClaimsIdentity(jwtToken.Claims));
        }
    } 

The JwtMiddleware from the affore-linked authentication scheme did not affect the Context object of the SignalR hub.

Instead of just the accountId, I took the validated JWT token and added an identity to the HttpContext User. This is probably not perfect but I hope it help someone in the future:

    var jwtToken = jwtUtils.ValidateJwtToken(token);
    if (jwtToken != null)
    {
        int? accountId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value);
        if (accountId != null)
        {
            // attach account to context on successful jwt validation
            context.Items["Account"] = await dataContext.Accounts.FindAsync(accountId);
            context.User.AddIdentity(new ClaimsIdentity(jwtToken.Claims));
        }
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文