Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

How to access context outside of hub? #1503

Closed
racamp101 opened this issue Feb 27, 2018 · 12 comments
Closed

How to access context outside of hub? #1503

racamp101 opened this issue Feb 27, 2018 · 12 comments

Comments

@racamp101
Copy link

racamp101 commented Feb 27, 2018

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.

@analogrelay
Copy link
Contributor

analogrelay commented Feb 27, 2018

Have you registered IHttpContextAccessor in the DI container? You have to register it manually: aspnet/Hosting#793

Ah, you mean the User property is not available. I misunderstood.

@analogrelay
Copy link
Contributor

The User property is populated by calling HttpContext.Authentication.AuthenticateAsync. SignalR will call this on your behalf for Hubs with the [Authorize] attribute, as will MVC actions/controllers with the [Authorize] attribute. If you want the user identity in other requests, you need to call it manually. The value is cached as well so it's safe to call it even if SignalR will run later in the middleware pipeline (SignalR will just use the cached value).

@davidfowl
Copy link
Member

@racamp101 where are you trying to get access to the user and which user?

@racamp101
Copy link
Author

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.

@davidfowl
Copy link
Member

@racamp101
Copy link
Author

I am using that but the user is only sometimes populated.

@davidfowl
Copy link
Member

What transport are you using and how are you asking for the user?

@analogrelay
Copy link
Contributor

that HttpContext.Authentication.AuthenticateAsync is async

Yes, you would need to use it from an async method.

Im trying to access it in a class that is injected into my hubs constructor.

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. IHttpContextAccessor is only really there as a stop-gap solution when you aren't able to use DI and method parameters to flow the data you need.

@analogrelay
Copy link
Contributor

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.

@realLiangshiwei
Copy link

I also encountered this problem. When using LongPolling, injecting IHttpContextAccessor in the hub and get user.Claims in httpcontext. sometimes loses information.

@davidfowl
Copy link
Member

Don't do that, use the GetHttpContext on the HubConnectionContext.

@realLiangshiwei
Copy link

I have defined a global shared session in my architecture. I need to set the value of the session when the request comes.I chose to use IHttpContextAccessor It works fine for mvc and webapi applications,

https://github.com/aspnetboilerplate/aspnetboilerplate/blob/e0ded5d8702f389aa1f5947d3446f16aec845287/src/Abp.AspNetCore/AspNetCore/Runtime/Session/AspNetCorePrincipalAccessor.cs#L11

but the context state is lost in signalr long polling mode. This will result in the correct value not being obtained using a custom session.
var userId = AbpSession.UserId;
image

Do you have any good solutions?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants