I am trying to get the current user from a context outside the controller in my web API. I use OpenIddict to authenticate users, so authorization is done using tokens.
I registered the HttpContextAcessor as singleton in my Startup.cs:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
After that, I insert the HttpContexrAcessor into my hub, for example:
public class NotificationHub : Hub { private IHttpContextAccessor _contextAccessor; private HttpContext _context { get { return _contextAccessor.HttpContext; } } public NotificationHub(IHttpContextAccessor contextAccessor) { _contextAccessor = contextAccessor; } public async Task JoinGroup() { Groups.Add(Context.ConnectionId, $"notification_{_context.User.Identity.Name}"); } public void Send(JsonResult notification) { Clients.Client(Context.ConnectionId).sendNotification(notification); } }
The problem is that _context.User.Identity.Name is always null.
Is there any way to do this?
source share