locked
Authenticate on SignalR console application from JS client RRS feed

  • Question

  • User1757586927 posted

    Hey there.
    Following scenario/my solution consists of the following: 

    Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries database with EF ).
    Project two: (CLIENT) I have an ASP.Net web application with an AngularJS client. 

    So far I can talk to the hub just fine. The problem is, I cannot seem to get the authentication to work. I've tried a bunch of things I've found but none of them worked. Most of them didn't even apply to my problem.. 

    Currently I've stripped my project back to the basics and I have the following code:

    Startup class:

        public class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.UseCors(CorsOptions.AllowAll);
                app.MapSignalR();
            }
        }

    My hub:

    [HubName("systemHub")]
        public class systemHub : Hub
        {
    
            public void Authenticate(String pLogin, String pPassword)
            {
                User lUser = new UserManager().Authenticate(pLogin, pPassword);
    //this is where I'd want to send the auth cookie or whatever and contact the "loginCallback" function in my client
            }
    
            [Authorize]
            public void Hello(String pMessage)
            {
                Clients.All.callbackFunc(pMessage);
            }
        }

    Js client:

    hinagApp.controller('hinagController', function ($scope) {
        $(document).ready(function () {
            var conURL = 'http://localhost:8080/signalr';
            $.getScript(conURL + '/hubs', function () {
                $.connection.hub.url = conURL;
                var lHub = $.connection.systemHub;
    
    lHub.client.callbackFunc = function(pM){
    alert(pM);
    }
    lHub.client.loginCallback = function (pSuccess) { if (pSuccess) { //if logged in lHub.server.hello("test"); } else { alert("fail"); } } $('#loginbutton').click(function () { lHub.server.authenticate($('#input_login').val(), $('#input_pass').val()); }); $.connection.hub.start(); }); }) });

    Monday, November 16, 2015 3:16 PM

All replies

  • User61956409 posted

    Hi SinnedB,

    Firstly, you could call a web service to check user's authentication status via jQuery AJAX, and you could start/stop connection in AJAX success callback function.

    $('#loginbutton').click(function () {
        $.ajax({
            type: "Post",
            url: "yoururl",
            data: "{'username and password'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (Authenticated) {
                    //start connection
                    $.connection.hub.start();
                }
            },
            error: function (err) {
                alert(err);
            }
        })
    }) 
    

    Secondly, if you’d like to restrict which users or roles can access hub methods, please refer to this link.

    http://www.asp.net/signalr/overview/security/hub-authorization

    Best Regards,

    Fei Han

    Tuesday, November 17, 2015 1:51 AM
  • User1757586927 posted

    Which webservice? There is none or maybe I just don't understand.. 

    I only have a console application ( SignalR ) and the asp.net which acts as the client. Do I need to make another ASP.Net site to handle the authentication and how will SignalR know about it?

    Tuesday, November 17, 2015 4:12 AM