我正在尝试通过查询字符串访问SignalRHub类上的令牌。 这是客户端代码:
const connection = new signalR.HubConnectionBuilder()
.withUrl($("#url").val(), {
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets,
accessTokenFactory: () => {
token
}
})
.configureLogging(signalR.LogLevel.Trace)
.build();
Jwt配置:
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
var path = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(accessToken) && (path.Equals("/websocket"))
{
context.Token = accessToken;
}
return Task.CompletedTask;
}
问题是,当我向我的集线器发出请求时,即使我通过accessTokenFactory传递了accessToken,它也为null。没有查询字符串参数“ access_token”。代码是在docs guide之后编写的。我已经尝试在启动时更改中间件调用,但没有解决。
启动文件:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddSessionStateTempDataProvider();
/*aditional configurations*/
services.AddSignalR();
/*more aditional configurations*/
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(options =>
{
options.WithExposedHeaders("Location");
options.AllowAnyOrigin();
options.AllowAnyHeader();
options.AllowAnyMethod();
options.AllowCredentials();
});
app.UseSignalR(routes =>
{
routes.MapHub<SignalRHub>("/websocket");
});
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute("api", "{controller}/{action}/{id}");
});
/*aditional middlewares*/
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
}
任何帮助都会被重视
答案 0 :(得分:0)
看起来您实际上并没有在accessTokenFactory实现中提供令牌。您需要返回令牌值,否则您的HubConnection将无法获取
accessTokenFactory: () => {
return token
}