【发布时间】:2020-06-01 15:13:35
【问题描述】:
我正在 ASP.NET Core 2.2 服务器应用程序上实现 Windows 身份验证,除 Angular 8 应用程序中的 SignalR 外,一切正常。我收到的错误如下:
从源“http://localhost:2302”访问“http://localhost:2110/api/aircrafts/hub/negotiate”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“Access-Control-Allow-Origin”标头出现在请求的资源上。
localhost:2302 是使用 @aspnet/signalr@1.1.4 和 localhost:2110 运行 Angular 8 的客户端部分 是服务器部分。这是我的服务器代码:
Startup.cs 配置方法
app.UseCors("CORS");
app.UseAuthentication();
var relativeUri = webSettings.SignalRAircraftsUri.ToString();
app.UseSignalR(hubs => { hubs.MapHub<AircraftsHub>(relativeUri); });
Startup.cs ConfigureServices 方法
services.AddCors(options =>
{
options.AddPolicy("CORS", builder =>
{
builder
.AllowAnyMethod()
.AllowAnyHeader()
.WithOrigins(webSettings.AllowedHosts.ToArray())
.AllowCredentials();
});
});
services.AddSignalR();
services.AddAuthentication(IISDefaults.AuthenticationScheme);
webSettings.AllowedHosts 包含 localhost:2302。
launchsettings.json:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:2110",
"sslPort": 0
}
},
这是 Angular 代码:
const signalRConnection = new signalR.HubConnectionBuilder().withUrl(url).build();
signalRConnection.start();
其中 url 是 localhost:2110/api/aircrafts/hub
任何帮助将不胜感激。
【问题讨论】:
标签: angular authentication cors signalr