0

I've went through almost all the posts regarding this issue 2 times to make sure I didn't miss something. I still don't get this to work though.

I have a Console SignalR Hub using Microsoft.AspNet.SignalR.Core I have a console SignalR Client connecting to the server, and I need to send some messages. When I connect to the server with GlobalHost.HubPipeline.RequireAuthentication(); in the server configuration, then I get a 401: Unauthorised error back. If I take it out, it works, but Context.User.Identity.Name is empty in the OnConnect.

Is there still something that I missed?

Here is my code: Hub

namespace SignalRService
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://*:8081";
            using (WebApp.Start(url))
            {
                Console.WriteLine("Server running on {0}", url);
                Console.ReadLine();
            }
        }
    }

    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
            GlobalHost.HubPipeline.RequireAuthentication();
        }
    }

    public class MyHub : Hub
    {
        public ConnectionMapping<string> connections = new ConnectionMapping<string>();
        public override Task OnConnected()
        {
            connections.Add(Context.User.Identity.Name, Context.ConnectionId);
            return Clients.All.joined(GetAuthInfo());
            //return base.OnConnected();
        }
        protected object GetAuthInfo()
        {
            var user = Context.User;
            return new
            {
                IsAuthenticated = user.Identity.IsAuthenticated,
                IsAdmin = user.IsInRole("Admin"),
                UserName = user.Identity.Name
            };
        }

        public void SendToUser(string UserName, string name, string Message)
        {
            //Clients.User(UserName).send(name, Message);
        }

        public void Send(string name, string message)
        {
            Clients.All.addMessage(name, message);
        }
    }
}

Client using Microsoft.AspNet.SignalR.Client

namespace ConsoleApp5
{
    class Program
    {
        private static void Main(string[] args)
        {
            //Set connection
            var connection = new HubConnection("http://127.0.0.1:8081/");
            //Make proxy to hub based on hub name on server
            connection.Credentials = CredentialCache.DefaultCredentials;

            var myHub = connection.CreateHubProxy("MyHub");
            //Start connection

            //myHub.On<>
            myHub.On<string, string>("addMessage", (param1, param2) =>
            {
                Console.WriteLine($"{param1}:  {param2}");
            });

            connection.Start().ContinueWith(task =>
            {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error opening the connection:{0}",
                                      task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine("Connected");
                }

            }).Wait();



            myHub.Invoke<string>("Send", "Peter Pan", "HELLO World ").ContinueWith(task => {
                if (task.IsFaulted)
                {
                    Console.WriteLine("There was an error calling send: {0}",
                                      task.Exception.GetBaseException());
                }
                else
                {
                    Console.WriteLine(task.Result);
                }
            });

            Console.Read();
            connection.Stop();
        }
    }
}

I've added this in on the server, but it did not help

    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public class AuthorizeClaimsAttribute : AuthorizeAttribute
    {
        protected override bool UserAuthorized(System.Security.Principal.IPrincipal user)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var principal = (ClaimsPrincipal)user;

            if (principal != null)
            {
                Claim authenticated = principal.FindFirst(ClaimTypes.Authentication);
                return authenticated.Value == "true" ? true : false;
            }
            else
            {
                return false;
            }
        }
    }
Jaques
  • 2,215
  • 1
  • 18
  • 35
  • And what name do you expect? Of a windows user? – Evk Dec 11 '17 at 20:12
  • Yes, tbh it does not really matter. At this stage it is null, so Windows user. A logged on user I will do with a cookie I believe, and I saw some examples of that, but I need to understand why this fails. – Jaques Dec 12 '17 at 06:53

0 Answers0