我想让CORS使用新的WebAPI项目。该项目只是使用ActiveDirectoryBearerAuthentication
的默认WebAPI模板(即具有MVC和WebAPI参考)。使用WebAPI启用CORS
每当我试图让我的API请求,我遇到了以下错误:
XMLHttpRequest cannot load https://localhost:44385/api/values. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:44369' is therefore not allowed access.
我已经安装Microsoft.Owin.Cors和Microsoft.AspNet.WebApi.Cors。
我WebApiConfig是这样的:
public static void Register(HttpConfiguration config)
{
config.EnableCors();
....
我Startup.Auth是这样的:周围的净
public void ConfigureAuth(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
....
解决方案说补充:
<customHeaders>
<!-- Adding the following custom HttpHeader will help prevent CORS from stopping the Request-->
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
到web.config 。但是,这样做只是允许所有内容,而不管Microsoft.AspNet.WebApi.Cors
是否已安装,启用或不可用。
难道你不希望你允许一切,或者你仍然想限制访问和阻止一些其他请求 –
有一点要知道的是,IE需要在响应中包含P3P头以便CORS工作 – pm100
我想阻止除一个域之外的所有内容。我以为我会倒退,以确保它正在工作。如果我没有做到这一点,我会认为web.config解决方案解决了它 - 它不是只是允许所有的东西,无论CORS配置。 – Mardoxx