ASP.NET 核心:Windows 带有 OPTIONS 异常的身份验证(CORS 预检)
ASP.NET Core: Windows Authentication with OPTIONS exception (CORS preflight)
我正在开发单页 Web 应用程序。它有 ASP.NET Core 3 后端和 Angular 9 前端。我 运行 在 Visual Studio 中的后端,在 IIS Express 上,在 http://localhost:59280. The front end runs in Visual Studio Code, using ng serve
, at http://localhost:4200。之前我不需要在后端开启 CORS,因为我只在 Chrome 中测试了应用程序,添加 --disable-web-security
命令行参数就足以关闭同源策略.在live服务器上不需要CORS,上面的跨域情况只出现在我的开发机上
现在我想在 Firefox 中调试前端,但由于无法关闭 Firefox 的同源策略,我必须在后端打开 CORS。不幸的是,它不起作用,因为我使用 Windows 身份验证,并且它会停止默认情况下未经身份验证的 CORS preflight request。如果我可以 让 HTTP OPTIONS 请求在没有 Windows 身份验证 的情况下被处理,这个问题就可以解决。我认为这可以通过向 web.config:
添加类似的内容来完成
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
<authorization>
<add accessType="Allow" verbs="OPTIONS" users="*" />
</authorization>
</security>
</system.webServer>
...但我收到一条错误消息:"This configuration section cannot be used at this path. This happens when the section is locked at a parent level." 显然 web.config 与 launchSettings.json 冲突,后者似乎控制Visual Studio 中的 IIS Express 后端为 运行 时的身份验证,使用以下两行:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
...
我不知道如何仅使用 launchSettings.json.
为 HTTP OPTIONS 请求单独关闭 Windows 身份验证
有没有办法在 ASP.NET Core 3 应用程序中为 HTTP OPTIONS 请求单独关闭 Windows 身份验证?
1) 上述web.config设置有效,我只需要解锁.vs目录中applicationhost.config中的"anonymousAuthentication"部分:<section name="anonymousAuthentication" overrideModeDefault="Allow" />
。 launchSettings.json 中 "anonymousAuthentication" 参数的值无关紧要。
2)按照@MartinStaufcik的建议,我在StartUp.Configure()的开头添加了一个中间件,用于响应预检请求(MDN ):
app.Use(async (context, next) => {
if (context.Request.Method == "OPTIONS") {
context.Response.StatusCode = 204;
context.Response.Headers.Add("Access-Control-Allow-Origin", context.Request.Headers["Origin"]);
context.Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
context.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
return;
}
await next();
});
3) 我还必须将 { withCredentials: true }
添加到 Angular 9 前端的 HttpClient.post() 的参数中。如果没有这个,OPTIONS 请求得到 204,但随后的 POST 得到 401.
我正在开发单页 Web 应用程序。它有 ASP.NET Core 3 后端和 Angular 9 前端。我 运行 在 Visual Studio 中的后端,在 IIS Express 上,在 http://localhost:59280. The front end runs in Visual Studio Code, using ng serve
, at http://localhost:4200。之前我不需要在后端开启 CORS,因为我只在 Chrome 中测试了应用程序,添加 --disable-web-security
命令行参数就足以关闭同源策略.在live服务器上不需要CORS,上面的跨域情况只出现在我的开发机上
现在我想在 Firefox 中调试前端,但由于无法关闭 Firefox 的同源策略,我必须在后端打开 CORS。不幸的是,它不起作用,因为我使用 Windows 身份验证,并且它会停止默认情况下未经身份验证的 CORS preflight request。如果我可以 让 HTTP OPTIONS 请求在没有 Windows 身份验证 的情况下被处理,这个问题就可以解决。我认为这可以通过向 web.config:
添加类似的内容来完成<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
<authorization>
<add accessType="Allow" verbs="OPTIONS" users="*" />
</authorization>
</security>
</system.webServer>
...但我收到一条错误消息:"This configuration section cannot be used at this path. This happens when the section is locked at a parent level." 显然 web.config 与 launchSettings.json 冲突,后者似乎控制Visual Studio 中的 IIS Express 后端为 运行 时的身份验证,使用以下两行:
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
...
我不知道如何仅使用 launchSettings.json.
为 HTTP OPTIONS 请求单独关闭 Windows 身份验证有没有办法在 ASP.NET Core 3 应用程序中为 HTTP OPTIONS 请求单独关闭 Windows 身份验证?
1) 上述web.config设置有效,我只需要解锁.vs目录中applicationhost.config中的"anonymousAuthentication"部分:<section name="anonymousAuthentication" overrideModeDefault="Allow" />
。 launchSettings.json 中 "anonymousAuthentication" 参数的值无关紧要。
2)按照@MartinStaufcik的建议,我在StartUp.Configure()的开头添加了一个中间件,用于响应预检请求(MDN ):
app.Use(async (context, next) => {
if (context.Request.Method == "OPTIONS") {
context.Response.StatusCode = 204;
context.Response.Headers.Add("Access-Control-Allow-Origin", context.Request.Headers["Origin"]);
context.Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
context.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
return;
}
await next();
});
3) 我还必须将 { withCredentials: true }
添加到 Angular 9 前端的 HttpClient.post() 的参数中。如果没有这个,OPTIONS 请求得到 204,但随后的 POST 得到 401.