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