Home > Web Front-end > JS Tutorial > How to Enable Cross-Origin Resource Sharing (CORS) on IIS7: A Step-by-Step Guide

How to Enable Cross-Origin Resource Sharing (CORS) on IIS7: A Step-by-Step Guide

Susan Sarandon
Release: 2024-10-27 02:07:30
Original
621 people have browsed it

How to Enable Cross-Origin Resource Sharing (CORS) on IIS7: A Step-by-Step Guide

Enabling Cross-Origin Resource Sharing on IIS7: A Comprehensive Guide

Introduction

Cross-Origin Resource Sharing (CORS) allows resources from one domain to be fetched and utilized by applications from a different origin. To enable CORS on IIS7, follow these steps:

Configuration

  1. Add Custom Headers:

    • Navigate to the web.config file for the hosting domain.
    • Add the following custom headers within the section:
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
    </customHeaders>
    Copy after login

Troubleshooting

Despite the configuration, if you still receive a 405 response, it may be due to IIS7's handling of HTTP OPTIONS.

Option 1: Modify IIS7 Handler Mappings

  1. Open IIS Manager.
  2. Navigate to Handler Mappings.
  3. Locate "OPTIONSVerbHandler."
  4. Change "ProtocolSupportModule" to "IsapiHandler."
  5. Set the executable to:

    %windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll
    Copy after login

Option 2: Handle OPTIONS Verb in Code

  1. Override the BeginRequest method in your application code:

    protected void Application_BeginRequest(object sender,EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    
        if(HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            // Handle pre-flight OPTIONS call from browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000" );
            HttpContext.Current.Response.End();
        }
    }
    Copy after login

Conclusion

Enabling CORS on IIS7 requires both configuration updates and potential troubleshooting. By following the steps outlined above, you can empower cross-origin interactions with confidence.

The above is the detailed content of How to Enable Cross-Origin Resource Sharing (CORS) on IIS7: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template