CORS in IIS issue with credentials and wildcard in Access-Control-Allow-Origin

空扰寡人 提交于 2019-11-29 15:38:54

The question doesn’t show the client code which is sending the request that causes that error, but:

The client code must be using XHR or the Fetch API (or using jQuery or other library that calls one of those), and that code is either setting the XHR withCredentials property to true or is calling the Fetch Request constructor with an options object having the credentials option set to include.

When that’s the case and the server response has an Access-Control-Allow-Origin: * header, your browser will log the error cited in the question.

So one solution is to change the JavaScript client code so it’s not setting XHR withCredentials to true and not calling the Fetch Request constructor with credentials: 'include'.

Another solution is to make your server-side code take the Origin request-header value and echo it to theAccess-Control-Allow-Origin response-header value.

For IIS, you can do that with the URL Rewrite Module by adding the following to your IIS config file (Web.config or ApplicationHost.config file in %SystemDrive%\inetpub\wwwroot\).

<configuration> 
    <system.webServer> 
        <rewrite> 
            <rules> 
                <rule name="Capture Origin Header"> 
                    <match url=".*" /> 
                    <conditions> 
                        <add input="{HTTP_ORIGIN}" pattern=".+" /> 
                    </conditions> 
                    <serverVariables> 
                        <set name="CAPTURED_ORIGIN" value="{C:0}" /> 
                    </serverVariables> 
                    <action type="None" /> 
                </rule> 
            </rules> 
            <outboundRules> 
                <rule name="Set-Access-Control-Allow-Origin for known origins"> 
                    <match serverVariable="RESPONSE_Access-Control-Allow-Origin"
                           pattern=".+" negate="true" /> 
                    <action type="Rewrite" value="{CAPTURED_ORIGIN}" /> 
                </rule> 
            </outboundRules> 
        </rewrite> 
    </system.webServer> 
</configuration>

Then remove whatever other existing code/config is setting Access-Control-Allow-Origin: *.

Note: The above is a modified version of the example config file in the step-by-step guide Enable CORS for specific domains in IIS using URL Rewrite.

I had the same issue, I had both http and https bindings on one website with Access-Control-Allow-Origin: * in this case http request will fail with the above error. you can create a separate website for http binding and remove Access-Control-Allow-Origin header from it.

Just to close the loop on this. The answers and suggestions posted above worked fine for an IIS-only website in regards to this issue.

My problem was some bug in the IIS-Node library that ultimately forced me down the path of migrating the entire stack to NodeJS and running all of the modules natively direct in Node.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!