SignalR Context.User.Identity.Name在控制台客户端/服务器中为空

时间:2017-12-11 19:35:12

标签: c# signalr windows-security

我已经完成了两次关于这个问题的几乎所有帖子,以确保我没有错过任何东西。我仍然不能让它工作。

我有一个使用Microsoft.AspNet.SignalR.Core的Console SignalR Hub我有一个控制台SignalR Client连接到服务器,我需要发送一些消息。当我在服务器配置中使用GlobalHost.HubPipeline.RequireAuthentication();连接到服务器时,我收到401:未经授权的错误。如果我将其删除,则可行,但Context.User.Identity.NameOnConnect为空。

还有什么我错过的吗?

这是我的代码: 的集线器

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);
        }
    }
}
使用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();
        }
    }
}

我已将此添加到服务器上,但它没有帮助

    [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;
            }
        }
    }

0 个答案:

没有答案