CORS发出IIS 8 Windows Server 2012 [英] CORS issue IIS 8 Windows Server 2012

查看:99
本文介绍了CORS发出IIS 8 Windows Server 2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:
我有两个应用程序,一个是 SPA Web应用程序,另一个是 Web API 2.0,它部署在Windows Server 2012的IIS 8.0上。 / p>

错误:
网站可以访问,并且可以在同一台机器上正常运行,但不能从外部访问,网页可以正确加载但在ajax上对该API的调用会产生以下错误...



跨域请求被阻止:同源起源策略禁止读取位于的远程资源。可以通过将资源移到同一域或启用CORS来解决此问题。



在控制器级别,我添加了以下属性:
[EnableCors(来源: ,标头: ,方法: *,SupportsCredentials = true)]



也已启用它在 WebApiConfig.cs

 公共静态无效寄存器(HttpConfiguration配置)
{
// Web API配置和服务

// Web API路由
config.MapHttpAttributeRoutes();
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: ActionApi,
routeTemplate: api / {controller} / {action} / {id},
默认值:new {id = RouteParameter.Optional,action = DefaultAction}
);

config.EnableCors(new EnableCorsAttribute( *, *, *)));
}

web.config 服务器配置是这样的。

 < system.webServer>。 
< handlers>
<删除名称= ExtensionlessUrlHandler-Integrated-4.0 />
<删除名称= OPTIONSVerbHandler />
<删除名称= TRACEVerbHandler />
< add name = ExtensionlessUrlHandler-Integrated-4.0 path = *。动词= * type = System.Web.Handlers.TransferRequestHandler preCondition = integratedMode,runtimeVersionv4.0 />
< / handlers>
< /system.webServer>

以下是 ajax 通话的摘要。

  $。ajax({
type:'get',
url:webApiUrl,
成功:函数(响应){
//这里一些逻辑
},
错误:函数(响应){
//一些逻辑此处
},
xhrFields:{
withCredentials:true
},
crossDomain:true
});




更多详细信息:




我已经在Windows Server 2008 R2的IIS 7.5上部署了相同的网站,并且运行正常,没有任何问题。



第二,我也尝试通过在IIS 8中添加以下响应标头。
Access-Control-Allow-Headers-内容类型
Access-Control-Allow-Methods-POST,GET,OPTIONS



还尝试将web.config中的以下值更改为





到目前为止,这些都不对我有用。寻找更多帮助...



已修复::只是防火墙阻止了托管的Web api端口:(

解决方案

只是防火墙阻止了托管的Web api端口


SCENARIO: I have two applications, one is "SPA web application" and the other one is "Web API 2.0" which is deployed on IIS 8.0 on Windows Server 2012.

ERROR: Website is accessible and working fine on the same machine but not from outside, web page is loaded properly but on ajax call to the API generates the following error...

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at . This can be fixed by moving the resource to the same domain or enabling CORS.

On controller level I have added the following attribute: [EnableCors(origins: "", headers: "", methods: "*", SupportsCredentials = true)]

Also enabled it in WebApiConfig.cs

public static void Register(HttpConfiguration config)
    {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "ActionApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional, action = "DefaultAction" }
            );

            config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
    }

web.config server configuration is like this..

<system.webServer>
   <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <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>  

and following is the snippet of the ajax call..

$.ajax({
        type: 'get',  
        url: webApiUrl,
        success: function (response) {
            // some logic here
        },
        error: function (response) {
            // some logic here
        },
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true
    });

FURTHER DETAILS:

I have deployed the same website on IIS 7.5 on a Windows Server 2008 R2 and it is working fine without any issue.

Secondly I have also tried by adding following response headers in IIS 8.. Access-Control-Allow-Headers - Content-Type Access-Control-Allow-Methods - POST,GET,OPTIONS

Also tried to change following value in web.config

to

None of these have worked for me so far. Looking for further help...

FIXED :: That was just the firewall blocking the hosted web api port :(

解决方案

just the firewall blocking the hosted web api port

这篇关于CORS发出IIS 8 Windows Server 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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