문제

is there any way to get current signalR request user outside the hub? I can use Hub.Context.User inside of hub methods, but if hub method calls any other underlying layer? Wcf service call - an additional BehaviorExtensionElement is used to add wcf message header with current user identity name.

public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
     request.Headers.Add(MessageHeader.CreateHeader(
        Constants.HeaderNames.SessionId, 
        Constants.HeaderNames.HeaderNamespace,
       _hubManager.ResolveHub(Constants.Hubs.MessengerHub).
                   Context.User.Identity.Name));
}

Yes, i found that the DefaultHubManager gets the Hub, but i'm not sure it will be the hub from current request, not the concurrent one or a new one, 'cause at the end of ResolveHub i see the following code runs

   return (DependencyResolverExtensions.Resolve(this._resolver, descriptor.HubType) 
       ??  Activator.CreateInstance(descriptor.HubType)) as IHub;

Obviuosly i can pass user from hub method to wcf call but it requires refactoring to move from wcf behaviour to setting request field with user name explicitly. Also can i rely on HttpContext.Current to get the info from cookie?

도움이 되었습니까?

해결책

No you cannot. The only way to retrieve the currently active user outside of the hub itself would be to pass the users information to whatever method you call in the hub.

A common pattern is to track your users by adding them to some sort of dictionary in OnConnected and removing them in OnDisconnected. You can then have an entirely separate way of identifying your users while having required information that's associated with them.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top