如何解决当某些用户使用WebSockets或ServerSentEvents时,SignalR提供404,而其他用户则不使用
在尝试为某些用户连接时,SignalR给了我404。除了access_token之外,URL相同。
每个用户都是稳定的可复制的(我的意思是有些用户稳定OK,某些用户稳定404)。
access_token解析了jwt diff(左为OK用户,右为404):
注意:黑色正方形下的URL相同。
前端是带有包"@microsoft/signalr": "^3.1.8"
的Angular 9,下面是构建连接的代码:
private buildHub(): HubConnection {
console.log(this.authService.accessToken);
let builder = new HubConnectionBuilder()
.withAutomaticReconnect()
.configureLogging(LogLevel.Information)
.withUrl('ws/notificationHub',{
accessTokenFactory: () => this.authService.accessToken
});
if (this.debugMode) {
builder = builder.configureLogging(LogLevel.Trace);
}
return builder.build();
}
后端正在使用“启动”中的下一个代码来配置signalR集线器:
在public void ConfigureServices(IServiceCollection services)
中:
services.AddSignalR()
.AddJsonProtocol(options =>
{
options.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver();
});
在public void Configure(IApplicationBuilder app,IHostingEnvironment env)
中:
app.UseSignalR(route =>
{
route.MapHub<NotificationHub>("/ws/notificationHub");
});
我们也使用自定义身份验证,因此我们为Hub类具有Authorize
属性:
[Authorize]
public class NotificationHub: Hub<INotificationHubClient>
以及public void ConfigureServices(IServiceCollection services)
中的这段代码:
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = identityServerSettings.Url;
options.Audience = identityServerSettings.ApiScopeName;
options.RequireHttpsMetadata = identityServerSettings.RequireHttpsMetadata;
options.Events = new Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerEvents
{
OnMessageReceived = context =>
{
var accessToken = context.Request.Query["access_token"];
var path = context.HttpContext.Request.Path;
if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/ws"))
{
context.Token = accessToken;
}
return Task.CompletedTask;
}
};
});
不幸的是,我没有可复制环境的完全访问权限,但是我可以要求查看任何设置或尝试进行一些更改。
我还能尝试解决什么问题?
更新:协商对两个用户都适用。
解决方法
在我的JWT规模增加之后,我最近遇到了这个问题。我发现我的情况是IIS引发404错误,因为查询字符串超出了2048个限制。增加查询字符串的最大长度后,我的问题得以解决。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。