【发布时间】:2018-08-02 20:08:16
【问题描述】:
我正在asp.net mvc 中处理signalR,我想制作signalR 客户端。我确实调用了 signalR 集线器确实存在并获得授权的 apis 服务器。当我删除授权时调用会进入 signalR 集线器,否则当我使用 javacsript 建立 signalR 连接时,signaR 返回 unathorized。
基本上我使用的是基于令牌的身份验证。我将带有查询字符串的令牌发送到 apis 服务器。但是我没有找到授权signalR的解决方案?
app.Map("/signalr", map =>
{
// Setup the CORS middleware to run before SignalR.
// By default this will allow all origins. You can
// configure the set of origins and/or http verbs by
// providing a cors options with a different policy.
map.UseCors(CorsOptions.AllowAll);
// SignalR Auth0 custom configuration.
map.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
{
Provider = new OAuthBearerAuthenticationProvider()
{
OnRequestToken = context =>
{
if (context.Request.Path.Value.StartsWith("/signalr"))
{
string bearerToken = context.Request.Query.Get("access_token");
if (bearerToken != null)
{
string[] authorization = new string[] { "bearer " + bearerToken };
context.Request.Headers.Add("Authorization", authorization);
}
}
return null;
}
}
});
var hubConfiguration = new HubConfiguration
{
// You can enable JSONP by uncommenting line below.
// JSONP requests are insecure but some older browsers (and some
// versions of IE) require JSONP to work cross domain
// EnableJSONP = true
};
// Run the SignalR pipeline. We're not using MapSignalR
// since this branch already runs under the "/signalr"
// path.
map.RunSignalR(hubConfiguration);
});
app.UseWebApi(config);
GlobalHost.DependencyResolver.Register(
typeof(SignalRHUB),
() => new SignalRHUB(new UnitOfWork(new DbFactory())));
//GlobalHost.HubPipeline.RequireAuthentication();
//app.MapSignalR();
我想通过使用 jwt 令牌来实现 signalR 授权。如果有人可以告诉我另一个解决方案。
【问题讨论】:
-
整理格式以获得更好的答案。
标签: asp.net-mvc asp.net-web-api signalr jwt