简体   繁体   中英

SignalR OWIN Authentication fails for MVC site

I'd created an MVC project using boilerplate OWIN authentication which was all working well.

I needed real-time signalling and setup a SignalR hub which was fully functioning, just without utilising the authenticated user.

When I added the [Authorize] attribute to the hub class and loaded the page that connects to the hub the Console in the F12 Developer Tools reported a 401 error with 'DENIED - The requested resource requires user authentication'.

If I didn't use the [Authorize] attribute the hub Context.User was null every time, so OnConnected and any methods I called didn't have Context.User set. This meant I couldn't work out which user was communicating.

All the other questions I'd found on StackOverflow had answers along the lines of "the Context.User will be set with the IPrincipal Identity details from the web page" which didn't help me as that wasn't happening for me.

The User was however set for all MVC controllers.

The problem, it turned out, was that in my main Startup.cs file I had:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        app.MapSignalR();
        ConfigureAuth(app);
    }
}

when what needed to happen was:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        ConfigureAuth(app);
        app.MapSignalR();
    }
}

So basically if the SignalR is mapped PRIOR to the OWIN authentication being setup then you don't get any OWIN handling for the SignalR... I of course only worked this out as I was writing the original question for posting on StackOverflow!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM