I was playing with the open authentication
in MVC5
and SignalR
. I use a javascript client to call a simple server method on SignalR
and receive a reply from Server. It works well, but if I add the [Authorize]
tag, it does not even call the server method (did not get any response while debugging).
My assumption was the server will use the Authentication mechanism to challenge the client. Am I missing anything? Do I have to manually authenticate the user from the client side and if so how do I pass the authentication token?
Here's my hub
:
[HubName("authChatHub")]
public class AuthChatHub : Hub
{
[Authorize]
public void Ping()
{
Clients.Caller.Pong("Connection is FINE!!");
Clients.Caller.Pong(Context.User == null
? "Null user"
: Context.User.Identity.IsAuthenticated.ToString());
}
}
Here's my Startup.Auth.cs
public void ConfigureAuth(IAppBuilder app)
{
app.UseGoogleAuthentication();
}
Here's the Startup.cs
, using the code to enable CORS.
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app); //added this after a suggestion here, not sure if this is the right place.
app.Map("/signalr", map =>
{
map.UseCors(CorsOptions.AllowAll);
var hubConfiguration = new HubConfiguration
{
// EnableJSONP = true //empty for now
};
map.RunSignalR(hubConfiguration);
});
}
}
And finally this client
side code calls the hub
method and listens to the server RPC.
this.sendMessage = () => {
this.authChat.server.ping();
};
this.authChat.client.pong = (message) => { console.log(message); };