首页 文章

Web API CORS支持不支持http

提问于
浏览
1

我在我的Web API控制器上启用了CORS支持,它在https上工作正常,但在http上我仍然得到这个o 'Access-Control-Allow-Origin'标头出现在请求的资源错误上 .

在WebApiConfig中,我使用此行启用了cors:

// Allow cross site javascript calls
config.EnableCors();

我的控制器类是这样介绍的:

[EnableCors("*","*","*")]
 public class ExternalDataController : ApiController

那么我如何在http和https上启用CORS?

在创建一个调用时,在azure原始http日志后面会出现以下行:

2015-05-06 05:53:15 ~1PROJECTX GET /diagnostics/settings X-ARR-LOG-ID=d5d487c0-8d32-4103-98cb-37fd7b022942 443 - 137.116.210.73 Portal-Exp/5.11.2.704+(Websites)+Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/537.36+(KHTML%2C+like+Gecko)+Chrome/42.0.2311.90+Safari/537.36+OPR/29.0.1795.47 - - projectx-development.scm.azurewebsites.net 200 0 0 733 1215 3921
2015-05-06 05:53:20 ~1PROJECTX POST /diagnostics/settings X-ARR-LOG-ID=599cb771-d399-49ee-89a2-6804af7c2712 443 - 137.116.210.73 Portal-Exp/5.11.2.704+(Websites)+Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/537.36+(KHTML%2C+like+Gecko)+Chrome/42.0.2311.90+Safari/537.36+OPR/29.0.1795.47 ARRAffinity=a32808ec0f09a64a5ce1705808bf8a2fb0c447c78db8498153641c9469a96a96 - projectx-development.scm.azurewebsites.net 204 0 0 500 1497 156
2015-05-06 05:53:21 ~1PROJECTX GET /diagnostics/settings X-ARR-LOG-ID=6d7bcfd4-aeeb-4c02-9f02-cf5bd54d2189 443 - 137.116.210.73 Portal-Exp/5.11.2.704+(Websites)+Mozilla/5.0+(Windows+NT+6.3;+WOW64)+AppleWebKit/537.36+(KHTML%2C+like+Gecko)+Chrome/42.0.2311.90+Safari/537.36+OPR/29.0.1795.47 ARRAffinity=a32808ec0f09a64a5ce1705808bf8a2fb0c447c78db8498153641c9469a96a96 - projectx-development.scm.azurewebsites.net 200 0 0 732 1301 78

2 回答

  • 0

    你用OWIN吗?只需在 Configuration 方法中启用CORS:

    public void Configuration(IAppBuilder app)
        {
            #region CORS
    
            app.UseCors(CorsOptions.AllowAll); 
    
            #endregion
    
  • 1

    对不起,我没有检查控制器级别 .

    我在WebApi配置级别有以下内容,它按预期工作 .

    var cors = new EnableCorsAttribute("*", "*", "*") ;
                config.EnableCors(cors);
    

    同样,如果您使用的是基本身份验证,请确保同时设置SupportsCredentials值

    var cors = new EnableCorsAttribute("*", "*", "*") { SupportsCredentials = true };
    

相关问题