nemo_87
nemo_87

Reputation: 4791

401 Unauthorized error or no message return in SignalR for WinForms

I am trying to create simple SignalR hub between MVC server side and WinForms client side.

I have created NotificationHub class, specified as this:

using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;

namespace PF.Timesheet.Service
{
    public class NotificationHub : Hub
    {
        public void Send(string name, string message)
        {
            Clients.All.broadcastMessage(name, message);
        }

        public override Task OnConnected()
        {

            return base.OnConnected();
        }
    }
}

While code on client side is this:

var hubConnection = new HubConnection("http://localhost:30341/singalr");
var chat = hubConnection.CreateHubProxy("NotificationHub");

string message2 = string.Empty;

chat.On<string, string>("broadcastMessage", (name, message) => { message2 = message; });

chat.On<string, string>("broadcastMessage", (name, message) =>
        this.Invoke((Action)(() =>
           RichTextBoxConsole.AppendText(String.Format("{0}: {1}" + Environment.NewLine, name, message)));

hubConnection.Start().Wait();

I was trying to get message from broadcastMessage on any possible way, both by assigning it to some string variable 'message2' or by appending text to rich textbox control on UI.

If I make call like this:

hubConnection.Start().Wait();

I will get 401 Unauthorized response from localhost where signalr is selfhosted. (local host is running in parallel as different solution within same project as client WinForms app).

What I am trying to do on server side is to push message to hub from code like this:

  var context = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
  context.Clients.All.broadcastMessage("NAM", "New entry created.");

If I remove Wait() from: hubConnection.Start(); I won't get Unauthorized 401 error, but I wouldn't get message value as well.

Does anybody sees what I am doing wrongly here?

Things I've tried so far:

  1. Trying to set up EnableDetailedErrors and EnableJSONP properties to true. I have read somewhere that SignalR hub has [Authorized] attribute by default and that this configuration should help.
    public class Startup
    {
       public void Configuration(IAppBuilder app)
       {
           HttpConfiguration config = new HttpConfiguration();
           WebApiConfig.Register(config);
           DependencyConfig.Initialize(config);
           Loging.Initialize();
           app.UseWebApi(config);
           app.MapSignalR(new HubConfiguration
           {
               EnableDetailedErrors = true,
               EnableJSONP = true
           });
        }
     }
  1. Trying to set WindowsAuth and AnonymousAuth properties for Server project to Enabled.

enter image description here

  1. Checking if message was actually pushed to hub when using:

    context.Clients.All.broadcastMessage("NAM", "New entry created.");

Message was there.

Did anybody passed issue with being Unauthorized and was able to read messages from MVC Server SignalR hub to WinForms client?

Upvotes: 1

Views: 3563

Answers (1)

nemo_87
nemo_87

Reputation: 4791

I managed to solve this on my own. Issue was that server (that was already setup before and not by me) had basic authentication with username and password. This wasn't visible at all with any [Authorization] attribute or anything, but it was still there.

I solved it by setting up Credentials property on hubConfiguration:

hubConnection.Credentials = new NetworkCredential("user.name", "password");

Authentication and connection to hub was successful and message was returned from server to client.

Upvotes: 0

Related Questions