문제

I am trying to setup a load test exercising as much of the code base as possible. I am running the server and client in the same process:

class Program 
{
    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR(new HubConfiguration
            {
                EnableDetailedErrors = true
            });
        }
    }
    public static void Main(string[] args)
    {
        var signalr = "http://localhost:21337/";

        using (WebApp.Start<Startup>(signalr))
        {
            var connection = new HubConnection(signalr) {Credentials = CredentialCache.DefaultNetworkCredentials};

            var catalog = connection.CreateHubProxy("CatalogHub");

            connection.Start().Wait();

            catalog.Invoke("SendChat", String.Empty, String.Empty, String.Empty);

            Console.ReadLine();
        }
    }
}

However, on connection.Start.Wait() I get a 401 Unauthorized error. I am not sure why because IIS is nowhere in the pipeline.

도움이 되었습니까?

해결책

PEBKAC. Turns out the hub has [Authorize] set on it but I was using anonymous authentication. Ensuring Ntlm authentication fixed the problem.

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var listener = (HttpListener)app.Properties[typeof(HttpListener).FullName];
            listener.AuthenticationSchemes = AuthenticationSchemes.Ntlm;
            app.MapSignalR(new HubConfiguration
            {
                EnableDetailedErrors = true,
                EnableJSONP = true
            });
        }
    }

다른 팁

I was getting the same error after I set the Authorize attribute on my hub class and found out that Windows Authentication was disabled in the properties for my Web API project. This was pointed out in getting 401 error with signalr owin host 2.0.3. This worked perfectly when I enabled it and I was also able to get the User Identity Name in my hub

One thing to note is that I did not have to set the Authentication scheme as mentioned in the above post

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top