Как отключить CORS проверку При подключении SignalR клиента (NetCore 3.1)
Возникла необходимость подключения к SignalR хабу большого кол-ва клиентов.
На продакшене будет заданна CORS политика отдельно для SignalR хаба, где пропишутся все разрешенные адреса клиентов
в методе WithOrigins("http://....", http://....")
Но для тестового сервера хотелось бы не указывать список WithOrigins а разрешить обращение со всех адресов.
Столкнулся с тем, что необходимо указывать AllowCredentials() иначе на клиенте будет ошибка подключения.
"Access to XMLHttpRequest at 'http://localhost:44138/providerHub/negotiate' from origin 'http://localhost:44111'
has been blocked by CORS policy: Response to preflight request doesn't pass access control check:
The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.
The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute."
Работающий вариант такой (обязательно указывать WithOrigins адреса):
app.UseCors(builder => builder
.WithOrigins("http://localhost:44111")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
Так не работает (нельзя использовать AllowAnyOrigin вместе с AllowCredentials)
app.UseCors(builder => builder
.AllowAnyOrigin
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
);
Т.е. на стороне сервера жесткие ограничения по настройки CORS (указывать WithOrigins + AllowCredentials)
.WithOrigins("http://localhost:44111")
.AllowCredentials()
На примитивном JS клиенте попытался отключить отправку Credentials в функции подключения,
указав connection.withCredentials = false; Это не помогло.
let hubUrl = 'http://localhost:44138/providerHub';
var connection = new signalR.HubConnectionBuilder()
.withUrl(hubUrl)
.configureLogging(signalR.LogLevel.Information)
.build();
//Функция подключения к хабу
async function start()
{
try {
if (connection.state === signalR.HubConnectionState.Disconnected) {
connection.withCredentials = false; // НЕ СРАБОТАЛО!!!
await connection.start();
document.getElementById("stateHub").innerHTML = "CONNECTION IsDone";
return console.info("CONNECTION IsDone");
}
else
{
return console.info("Already Connected !!!");
}
} catch (err)
{
document.getElementById("stateHub").innerHTML = "CONNECTION Errors: " + err.toString();
return console.error(err.toString());
}
};
Возможно, как-то по другому отключается