Asp.net web api 如何在WebAPI中完全禁用CORS
我们需要在WebAPI项目中禁用CORS,我已经在Asp.net web api 如何在WebAPI中完全禁用CORS,asp.net-web-api,cors,Asp.net Web Api,Cors,我们需要在WebAPI项目中禁用CORS,我已经在Startup.cs类和public void配置(IAppBuilder app)方法中注释掉了下面的行 app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 通过遍历,发送以下请求 curl -H "Origin: http://www.google.com" --verbose \ http://localhost:23422/api/values 响应 HTTP/
Startup.cs
类和public void配置(IAppBuilder app)
方法中注释掉了下面的行
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
通过遍历,发送以下请求
curl -H "Origin: http://www.google.com" --verbose \ http://localhost:23422/api/values
响应
HTTP/1/1 200 OK
Content-Type: application/json; charset=utf-8
Server: XXXX
X-SourceFiles: XXX
X-Powered-By: ASP.NET
[
"value1",
"value2"
]
HTTP/1/1 405 Method Not Allowed
Allow: GET,POST
Content-Type: application/json; charset=utf-8
Server: XXXX
X-SourceFiles: XXX
X-Powered-By: ASP.NET
{
"message": "The requested resource does not support http method "OPTIONS"."
}
它确实起作用并返回实际结果。这是否意味着CORS仍然受到支持?我假设它不会返回任何值,因为我从google.com请求它。
但是,当我尝试下面的请求时。它返回405方法不允许
curl -H "Access-Control-Request-Method: GET" -H "Origin: http://google.com" --head \ http://localhost:44312/api/values
响应
HTTP/1/1 200 OK
Content-Type: application/json; charset=utf-8
Server: XXXX
X-SourceFiles: XXX
X-Powered-By: ASP.NET
[
"value1",
"value2"
]
HTTP/1/1 405 Method Not Allowed
Allow: GET,POST
Content-Type: application/json; charset=utf-8
Server: XXXX
X-SourceFiles: XXX
X-Powered-By: ASP.NET
{
"message": "The requested resource does not support http method "OPTIONS"."
}
由于您没有在响应中获得allow cross origin标头(当您的请求具有标头时),因此这不能证明仍然启用了cross origin
假设禁用了跨原点,为什么仍要获取数据?因为CORS错误发生在internet浏览器中。阻止访问的是浏览器。使用C#、Powershell等编写的脚本仍然可以访问任何公共域中的资源,因为它在PC上运行,而不是在Internet浏览器中运行。由于您在响应中没有获得允许跨源标头(当您的请求有标头时),这并不能证明仍然启用了跨源
假设禁用了跨原点,为什么仍要获取数据?因为CORS错误发生在internet浏览器中。阻止访问的是浏览器。用C#、Powershell等编写的脚本仍然可以访问任何公共域中的资源,因为它在PC上运行,而不是在Internet浏览器中运行。这对我在ASP Net Core中很有效:
services.AddCors();
// global cors policy
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
//.WithOrigins("https://localhost:44351")); // Allow only this origin can also have multiple origins separated with comma
.AllowCredentials()); // allow credentials
我也有类似的问题
在startup.cs-ConfigureServices中:
services.AddCors();
// global cors policy
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
//.WithOrigins("https://localhost:44351")); // Allow only this origin can also have multiple origins separated with comma
.AllowCredentials()); // allow credentials
在startup.cs中-配置:
services.AddCors();
// global cors policy
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
//.WithOrigins("https://localhost:44351")); // Allow only this origin can also have multiple origins separated with comma
.AllowCredentials()); // allow credentials
安装NuGet软件包:Microsoft.AspNetCore.Cors
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
编辑:
我试图理解CORS,现在我就是这样理解的-如果我错了,请纠正我:
图片仅供参考-地址与文本中的地址不同
CORS:
services.AddCors();
// global cors policy
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true) // allow any origin
//.WithOrigins("https://localhost:44351")); // Allow only this origin can also have multiple origins separated with comma
.AllowCredentials()); // allow credentials
1。客户端请求从-https://localhost:5050
2.客户端获取该网页
3.客户端尝试从https://localhost:5050
通过使用POST或GET即可
4.我们在https://localhost:6060
我们希望将它与来自的网页一起使用-https://localhost:5050
5.客户端尝试从API-https://localhost:6060
6.客户端收到CORS错误消息-因为默认情况下只有源“地址”https://localhost:6060
是唯一允许获取,获取的代码,post等。如同一地址上的swagger等可以获取数据,但其他地址不能。但是,其他API在服务器上没有CORS配置的情况下如何可能使用此API。这是因为CORS与浏览器以及停止响应的浏览器相关,因此在没有CORS的情况下将其与其他API一起使用是没有问题的
7.要允许网页访问API,需要为此配置服务器
8。服务器需要添加一个访问控制允许来源:https:/address:port
头,并返回发送请求的允许来源“address”
9.在startup.cs
的配置方法中配置了ASP net Core:
// CORS - Allow calling the API from WebBrowsers
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
//.WithOrigins("https://localhost:44351")); // Allow only this origin can also have multiple origins seperated with comma
.SetIsOriginAllowed(origin => true));// Allow any origin
ASP net Core中的在startup.cs中的配置方法中配置:
// CORS - Allow calling the API from WebBrowsers
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
//.WithOrigins("https://localhost:44351")); // Allow only this origin can also have multiple origins seperated with comma
.SetIsOriginAllowed(origin => true));// Allow any origin
10。这意味着,当服务器返回响应时,它将添加访问控制允许来源:以及允许来源的地址
11。浏览器将得到响应,并将查找访问控制允许来源:
如果此标题存在且该值作为发送请求的地址的来源,则“网页地址”。然后通过浏览器允许响应
12.如果没有访问控制允许来源:头,这意味着服务器没有CORS配置,并且应该在使用浏览器的API数据之前进行配置其他API可以获取和发布数据,但客户端可以通过浏览器不能
13。如果存在访问控制允许来源:带有响应的标题,但该值不是当前地址,“网页地址”,则这意味着没有为此特定网站配置服务器
结论:
因此,需要配置服务器,以便网站使用API。这是默认的浏览器行为,将查找访问控制允许来源:标题如果不存在,浏览器将拒绝向客户端显示响应。如果访问控制允许来源:存在,但来源值与网站地址不同,浏览器将拒绝向客户端显示响应。这意味着在任何情况下,服务器都应该配置为