简体   繁体   English

在集线器之外获取SignalR用户(Hub.Context)

[英]Get SignalR User (Hub.Context) outside of hub

is there any way to get current signalR request user outside the hub? 有没有办法让当前的signalR请求用户在集线器外? I can use Hub.Context.User inside of hub methods, but if hub method calls any other underlying layer? 我可以在hub方法中使用Hub.Context.User,但是如果hub方法调用任何其他底层图层? Wcf service call - an additional BehaviorExtensionElement is used to add wcf message header with current user identity name. Wcf服务调用 - 附加的BehaviorExtensionElement用于添加具有当前用户标识名称的wcf消息头。

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 是的,我发现DefaultHubManager获取Hub,但我不确定它是当前请求的集线器,而不是并发的或新的,因为在ResolveHub结束时我看到以下代码运行

   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. 很明显,我可以将用户从hub方法传递给wcf调用,但它需要重构从wcf行为转移到设置具有用户名的请求字段。 Also can i rely on HttpContext.Current to get the info from cookie? 我也可以依靠HttpContext.Current从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. 一种常见的模式是通过将用户添加到OnConnected中的某种字典并在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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM