1
我有一些麻煩訪問我的userId後,並與connectionIds創建我的字典。我目前使用OAuthBearer爲我的用戶的認證和信息保存到ClaimsIdentity,類似如下:SignalR 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();
,則可以獲取userId。
任何幫助將不勝感激,因爲我已經堅持了很長一段時間。