【问题标题】:SignalR ClaimsIdentity Null Bearer tokenSignalR ClaimsIdentity Null Bearer 令牌
【发布时间】:2016-08-17 04:56:26
【问题描述】:

在访问我的 userId 并使用 connectionIds 创建我的字典之后,我遇到了一些问题。我目前正在使用 OAuthBearer 对我的用户进行身份验证并将信息保存到 ClaimsIdentity,如下所示:

            public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        using (AuthRepository _repo = new AuthRepository())
        {

            IdentityUser user = await _repo.FindUser(context.UserName, context.Password);
            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
            identity.AddClaim(new Claim("sub", context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id));


            var props = new AuthenticationProperties(new Dictionary<string, string>
            {
                { 
                    "as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
                },
                { 
                    "userName", context.UserName
                },
                { 
                    "userId", user.Id
                }
            });

            var ticket = new AuthenticationTicket(identity, props);
            context.Validated(ticket);
        }

    }

当我在我的启动/我的 signalR 集线器中,当我尝试使用 IRequest.user.identity.name 或 HttpContext.Current.User.Identity.GetUserId(); 时,我得到一个空值。

public string GetUserId(IRequest request)
        {
            var userId = request.User.Identity.Name;
            return userId.ToString();
        }

我认为我的声明有问题,因为我无法访问 UserId 和 userName 的值。如果我在其他任何地方运行HttpContext.Current.User.Identity.GetUserId();,我可以获得用户ID。

任何帮助将不胜感激,因为我已经坚持了很长一段时间。

【问题讨论】:

    标签: asp.net-web-api signalr claims-based-identity signalr-hub bearer-token


    【解决方案1】:

    我遇到了同样的问题,请尝试将 HttpContext.Current 替换为 Context.Request.GetHttpContext() 在您的 signalR 集线器方法中。

    根据MSDN,HubCallerContext 返回客户端的上下文。

    【讨论】:

      猜你喜欢
      • 2019-02-18
      • 1970-01-01
      • 2013-11-25
      • 2021-07-10
      • 2016-03-23
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      相关资源
      最近更新 更多