aspnet-contrib / AspNet.Security.OpenIdConnect.Samples

ASP.NET Core samples demonstrating how to use the OpenID Connect server with MVC or JS apps
64 stars 31 forks source link

Why SignalR SimpleConnection OnConnected doesn't fire? #18

Closed dreampper closed 7 years ago

dreampper commented 7 years ago

Hello, sorry for one more inconvinience :)

I'm using an Web API with Aurelia JS and I'm trying to understand the flow to use query string in SignalR connection. It's something new for me.

In the sample HelloSignalR we have no Hubs, so I'm trying to connect to the hub by my understand.

When I added only app.UseSignalR<SimpleConnection>("/signalr"); the SignalR log throw an error indicating that "it's not possible do load the hubs", so now I'm adding this two lines:

app.UseSignalR();
app.UseSignalR<SimpleConnection>("/signalr");

In my client, I'm connecting in that way:

connectToNotifications(){
        let tokenResponse = this.authService.getToken();
        tokenResponse.then(token => {
            let connectionPath = this.uri + "signalr";
            var connection = $.connection(connectionPath, "access_token=" + token);

            var log = (data) => { console.log(data); }

            connection.received( (data) => {
                log("From server: " + data);
            });

            connection.stateChanged( (change) => {
                switch (change.newState) {
                    case $.signalR.connectionState.reconnecting:
                        log('Re-connecting');
                        break;
                    case $.signalR.connectionState.connected:
                        log("Connected to " + connection.url);
                        break;
                    case $.signalR.connectionState.disconnected:
                        log("Disconnected");
                        break;
                }
            });
            connection.start().done( () => {
                console.log('SIGNALR CONECTOU');
                connection.send("Test message");

                $.connection.hub.logging = true;
                $.connection.hub.start().done(function () {
                    console.log('SIGNALR CONECTOU NO HUB');
                    notificationHub.server.joinGroup();

                });

                var notificationHub = $.connection.notificationHub;

                notificationHub.client.send = (result) => {
                    console.log('NOTIFICATION');
                    console.log(result);
                };
            });
        });

But in My hub, when notificationHub.server.joinGroup() is fired, the Context.User.Identity is still null.

That piece of code is working normally:

app.UseOAuthValidation(options =>
            {
                options.Events = new OAuthValidationEvents
                {
                    // Note: for SignalR connections, the default Authorization header does not work,
                    // because the WebSockets JS API doesn't allow setting custom parameters.
                    // To work around this limitation, the access token is retrieved from the query string.
                    OnRetrieveToken = context =>
                    {
                        context.Token = context.Request.Query["access_token"];

                        if (context.Token != null)
                        {
                            var a = context.Token;
                        }

                        return Task.FromResult(0);
                    }
                };
            });

But I have noticed that the methods on SimpleConnection are never fired and I'm thinking if It's necessary to the flow work.

kevinchalet commented 7 years ago

That's a question you should ask to the SignalR team, or to @DovydasNavickas who created the SignalR sample (that works fine on my machine, FWIW) :sweat_smile:

DovydasNavickas commented 7 years ago

Sorry for the delay.

The most probable reason that I see why you get no OnConnected hit on a Hub is because SimpleConnection is a PersistentConnection and not a Hub.

Never the less, this:

var identity = request.HttpContext.User.Identity;

Should still work normally. And I use this in production with PersistentConnection for more than a year, which is why I'm pretty sure it works 😅

kevinchalet commented 7 years ago

Closing, as the question was answered.

@DovydasNavickas thanks, as usual :+1: