asp.net Core SignalR 筆記
HttpRequest
httpRequest.Headers["key"]; //不會UrlDecode
httpRequest.Query["key"]; //會UrlDecode
CustomAuthorization 跟 Redis backplane
public class CustomAuthorizationRequirement : IAuthorizationRequirement
{
}
public class CustomAuthorizationHandler : AuthorizationHandler<CustomAuthorizationRequirement>
{
readonly IHttpContextAccessor _httpContextAccessor;
public CustomAuthorizationHandler(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CustomAuthorizationRequirement requirement)
{
if(isValid) context.Succeed(requirement);
else context.Fail();
return Task.CompletedTask;
}
}
//Startup.cs --ConfigureServices
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IAuthorizationHandler, CustomAuthorizationHandler>();
services.AddAuthorization(options =>
{
options.AddPolicy("CustomHubAuthorizatioPolicy", policy =>
{
policy.Requirements.Add(new CustomAuthorizationRequirement());
});
});
services.AddSignalR().AddStackExchangeRedis(Configuration["Radis:Url"], options => {
options.Configuration.ChannelPrefix = "MyApp";
}); ;
//Startup.cs --Configure
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<CommentsHub>($"/{nameof(CommentsHub)}", options =>
{
options.Transports = HttpTransportType.WebSockets;
});
});
設定LoadBalancer的有兩台主機的時候 會連線失敗時
可以設定 省略/negotiate
let connection = new signalR.HubConnectionBuilder()
.withUrl("/chathub", {
skipNegotiation: true,
transport: signalR.HttpTransportType.WebSockets
})
.build();
如果內容有誤請多鞭策謝謝