This is driving me insane.
I\'m using latest signalR release (2.0.2). This is my hub code (OnConnected)
public override Task OnConnected()
If you're mapping /signalr
as a 'branched pipeline' you need to do this. Make sure to use bp.UseCookieAuthentication
and not app
:
app.Map("/signalr", bp =>
{
bp.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/store/youRaccount/login")
});
Tip: I deliverately changed the casing so when I see youRaccount
in the URL bar I know it worked :-)
I found the final solution, this is the code of my OWIN startup class:
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
// Enable the application to use a cookie to store information for the signed i user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Home/Index")
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseMicrosoftAccountAuthentication(new MicrosoftProvider().GetAuthenticationOptions());
app.UseTwitterAuthentication(new TwitterProvider().GetAuthenticationOptions());
app.UseFacebookAuthentication(new FacebookProvider().GetAuthenticationOptions());
app.UseGoogleAuthentication(new GoogleProvider().GetAuthenticationOptions());
}
Making myself some coffee, I thought "What about mapping SignalR AFTER the authentication, and voila! Now it's workign as expected.
public void Configuration(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed i user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Home/Index")
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseMicrosoftAccountAuthentication(new MicrosoftProvider().GetAuthenticationOptions());
app.UseTwitterAuthentication(new TwitterProvider().GetAuthenticationOptions());
app.UseFacebookAuthentication(new FacebookProvider().GetAuthenticationOptions());
app.UseGoogleAuthentication(new GoogleProvider().GetAuthenticationOptions());
app.MapSignalR();
}
For the newer .NET Core SignalR the full instructions explain that when using websockets you need to manually pull out the accessToken from the query string. This is easy to miss.
https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-2.2
Basically where you call AddAuthentication()
you need to add AddJwtBearer()
and then set a handler for the OnMessageReceived
handler.
Search for 'OnMessageReceived' in the above link for the code. It's a bit gnarly in the sense that you even have to add this yourself - but that's why it's easy to miss too.
just make sure auth. configuration is called befor start app.MapMignalrR()
i changed this
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
ConfigureAuth(app);
}
}
to this
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.MapSignalR();
}
}
hugs ..
If you're using Web Api and SignalR in the same project, you have to map SignalR before registering Web Api.
Change this:
app.UseWebApi(GlobalConfiguration.Configuration);
app.MapSignalR();
To this:
app.MapSignalR();
app.UseWebApi(GlobalConfiguration.Configuration);