1
私はAsp.net Identityフレームワークを認証に使用しています。 接続されたクライアントのユーザーIDがconnectionIdまたは接続されたユーザーの役割から必要です。どのようにasp.netアイデンティティフレームワークSignalRのConnectionIdからUserIdを取得できますか?
私はAsp.net Identityフレームワークを認証に使用しています。 接続されたクライアントのユーザーIDがconnectionIdまたは接続されたユーザーの役割から必要です。どのようにasp.netアイデンティティフレームワークSignalRのConnectionIdからUserIdを取得できますか?
public class WhateverHub : Hub
{
public override Task OnConnected()
{
//Get the username
string name = Context.User.Identity.Name;
//Get the UserId
var claimsIdentity = Context.User.Identity as ClaimsIdentity;
if (claimsIdentity != null)
{
// the principal identity is a claims identity.
// now we need to find the NameIdentifier claim
var userIdClaim = claimsIdentity.Claims
.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);
if (userIdClaim != null)
{
var userIdValue = userIdClaim.Value;
}
}
return base.OnConnected();
}
}
は私がconnectionIDからユーザIDを必要とするあなたのHub
クラス
で
[Authorize]
属性を使用することを忘れないでください?あなたはsignalIでconnectionIdを知っていますか?接続されたクライアントに固有の接続IDがあり、それらのcoectiondId userIdが必要ですか?私が何を意味するのか理解していますか? –'SignalR ConnectiondId'と' Identity UserId'( 'Users'テーブルの主キー)は同じではありません。あなたは何が必要ですか? –
ありがとうございました:)その作品は今:)別の質問のために私を助けることができますか? –