【发布时间】:2014-02-07 09:29:24
【问题描述】:
我在MVC5 和SignalR 中使用open authentication。我使用 javascript 客户端在 SignalR 上调用一个简单的服务器方法并接收来自服务器的回复。它工作得很好,但是如果我添加[Authorize] 标签,它甚至不会调用服务器方法(调试时没有得到任何响应)。
我的假设是服务器将使用身份验证机制来挑战客户端。我错过了什么吗?我是否必须从客户端手动对用户进行身份验证,如果需要,我该如何传递身份验证令牌?
这是我的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());
}
}
这是我的Startup.Auth.cs
public void ConfigureAuth(IAppBuilder app)
{
app.UseGoogleAuthentication();
}
这是Startup.cs,使用代码启用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);
});
}
}
最后这个client 侧代码调用hub 方法并监听服务器RPC。
this.sendMessage = () => {
this.authChat.server.ping();
};
this.authChat.client.pong = (message) => { console.log(message); };
【问题讨论】:
标签: javascript authentication signalr asp.net-mvc-5