-
Notifications
You must be signed in to change notification settings - Fork 447
How to access context outside of hub? #1503
Comments
Ah, you mean the User property is not available. I misunderstood. |
The User property is populated by calling |
@racamp101 where are you trying to get access to the user and which user? |
that HttpContext.Authentication.AuthenticateAsync is async saying that sometimes I'm able to access the user is it possible its not finishing sometimes before the method I'm trying to access it in runs? Im trying to access it in a class that is injected into my hubs constructor. |
You would need to use the IHttpContextAccessor https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/ |
I am using that but the user is only sometimes populated. |
What transport are you using and how are you asking for the user? |
Yes, you would need to use it from an async method.
Have you considered passing the data into the service that your hub uses? If the Hub is the one consuming this service, it's the one with access to the user data. The service should accept it as input rather than loading it automatically from an additional dependency. |
Closing this as we haven't had any feedback since the last message. Please feel free to comment if you still have questions, we can always re-open or file a new issue if there's a problem to be resolved. |
I also encountered this problem. When using LongPolling, injecting IHttpContextAccessor in the hub and get |
Don't do that, use the GetHttpContext on the HubConnectionContext. |
I need to access the user outside the hub. I can user Context inside the hub just fine but if I try to use IHttpContextAccessor from a class the user is no longer available. Is there another way to get the correct context?
I see posts about injecting IHubContext but I cant access it this way I just need the context.user and using that would cause a circular ref.
The text was updated successfully, but these errors were encountered: