使用CORS的应用程序在Azure中不起作用 [英] Application using CORS does not work in Azure

查看:92
本文介绍了使用CORS的应用程序在Azure中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的应用程序配置为使用CORS。一切都可以在我的本地环境中完美运行,但是将应用程序部署到Azure后,我们会收到以下错误消息:

I have configured my application to use CORS. All works perfectly in my local environment, but after deploying the app to Azure, we receive the following error:


否'Access-Control-在请求的
资源上存在Allow-Origin标头。

No 'Access-Control-Allow-Origin' header is present on the requested resource.

当我在计算机上运行应用程序并创建使用DHC chrome扩展名的原始标头,它确实可以工作:

When I run the application on my computer and create the origin header using the DHC chrome extension, it does actually work:

Access-Control-Allow-Credentials:   true
Access-Control-Allow-Origin:        http://mysite.s3-website-eu-west-1.amazonaws.com
Access-Control-Expose-Headers:      Location,Finished

这是我使用的代码/配置:

This is the code/configuration that I use:

全局.asax.cs

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    config.EnableCors(new MyCorsPolicyAttribute());
    config.MapHttpAttributeRoutes();
}

MyCorsPolicyAttribute.cs

public class MyCorsPolicyAttribute : Attribute, ICorsPolicyProvider
{
    private CorsPolicy _policy;

    public MyCorsPolicyAttribute()
    {
        _policy = new CorsPolicy
        {                
            AllowAnyMethod = true,
            AllowAnyHeader = true,
            SupportsCredentials = true
        };

        _policy.ExposedHeaders.Add("Location");
        _policy.ExposedHeaders.Add("Finished");

        // Add allowed origins.
        _policy.Origins.Add("http://mysite.s3-website-eu-west-1.amazonaws.com");
        _policy.Origins.Add("http://localhost:81");//for testing purposes; 
   }
    public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request)
    {
        return Task.FromResult(_policy);
    }

    public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return Task.FromResult(_policy);
    }   
}

最后是 web.config

<system.webServer>
  <handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <remove name="WebDav" />
    <remove name="OPTIONSVerbHandler" />
    <remove name="TRACEVerbHandler" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>

为什么以上方法在Azure环境中不起作用?

Why doesn't the above work in the Azure enviroment?

推荐答案

自最近以来,Azure API应用程序(现在已过时,并已统一到App Services中)不遵循代码定义的CORS标头。部署到新的App Services实例。

Since recently Azure API apps (which are now obsolete and were unified into App Services) do not obey the CORS headers defined by code as yours. Deploy to a new App Services instance.

这篇关于使用CORS的应用程序在Azure中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆