question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AbpSession.UserId null in SignalR Hub

See original GitHub issue

Abp Package Version: Latest (Creating new template from site) Base Framework: .Net Core Steps need to reproduce: I have created a repo on GitHub that has a project to test. https://github.com/chadsmith12/SignalRTest/tree/master

But essentially when connecting to SignalR the following way:

new signalR.HubConnectionBuilder()
            .withUrl(abp.appPath + 'signalr-TestHub', {
                transport: signalR.HttpTransportType.LongPolling,
                accessTokenFactory: () => {
                    return 'CURRENT JWT TOKEN HERE';
                }
            }).build();

And using the example Hub from the documentation like so:

[AbpMvcAuthorize]
    public class TestHub : Hub, ITransientDependency
    {
        public IAbpSession AbpSession { get; set; }

        public ILogger Logger { get; set; }

        public TestHub()
        {
            AbpSession = NullAbpSession.Instance;
            Logger = NullLogger.Instance;
        }

        public async Task SendMessage(string message)
        {
            var userId = AbpSession.UserId;
            var connection = Context.User.Identity.GetUserId();
            await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", userId, message));
        }

        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
            Debug.WriteLine("UserId" +  AbpSession.UserId);
            Logger.Debug("A client connected to MyChatHub: " + Context.ConnectionId);
        }

        public override async Task OnDisconnectedAsync(Exception exception)
        {
            await base.OnDisconnectedAsync(exception);
            Logger.Debug("A client disconnected from MyChatHub: " + Context.ConnectionId);
        }
    }

And then connecting to a Hub that has the [AbpMvcAuthorize] attribute over the Hub. You will be authorized correctly but at random times the UserId inside of AbpSession will be null, even though Context.User.Identity.GetUserId(); will give you the correct UserId. You can look at the logs for SignalR and notice that JWT is being sent correctly it seems each time and you will always get authorized when sending the JWT, but it is just that the AbpSession is empty at random times.

I also had a stackoverflow post a couple days ago that describes it. https://stackoverflow.com/questions/53125573/aspnetboilerplate-signalr-jwt-authentication/

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
realLiangshiweicommented, Nov 8, 2018

Hi. This problem occurs when using LongPolling. The signalr caches the first request context. For later requests, if you are lucky enough to run in the HTTP request context, it may work. Abp uses IHttpContextAccessor to get information from the current request context. In signalr it depends on luck. 😂

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

So currently you can use other connection modes,If there are other solutions, I will continue to comment.

0reactions
toanmlcommented, Feb 24, 2020

Because mailiming’s solution didn’t work for me, I create another question https://github.com/aspnetboilerplate/aspnetboilerplate/issues/5314

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get the UserId from AbpSession in SignalR Hub
Properties UserId and TenantId are still null . Chat class is inside the "Application" layer where AbpSession is working well. c# ...
Read more >
Developers - AbpSession.UserId null in SignalR Hub -
AbpSession.UserId null in SignalR Hub ; Abp Package Version: Latest (Creating new template from site) ; Base Framework: .Net Core ; Steps need...
Read more >
How to get the userid of a user in Signalr hub
However, both user and userName are null even though a connection is granted by SignalR. How can I get the userName in this...
Read more >
AbpSession not injected #1165
Hi, I am implamanting SignalR Hub. ... Problem is that its not injected, only nullinstance is retrieved. ... Instance; AbpSession = NullAbpSession.
Read more >
CurrentUser.Id is null connecting to AbpHub #3499
I have a third-party application that is using an access token to access the Web API method. It is working well so far....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found