Home > Article > Web Front-end > How do I enable Cross-Origin Resource Sharing on IIS 7?
Enabling Cross-Origin Resource Sharing on IIS 7
Cross-Origin Resource Sharing (CORS) is a mechanism that allows client applications to access resources from different domains. By default, browsers restrict cross-origin requests for security reasons. To enable CORS on IIS 7, follow these steps:
Configure the Web.config file:
Add the following custom headers to the
<code class="xml"><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></code>
Handle HTTP OPTIONS requests:
By default, IIS 7 handles HTTP OPTIONS requests. To allow your application to handle these requests, modify the protocol support module for the 'OPTIONSVerbHandler' in IIS Manager:
Alternatively, respond to HTTP OPTIONS in code:
Add the following code to the Application_BeginRequest method in your application:
<code class="csharp">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 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(); } }</code>
By following these steps, you can enable CORS on IIS 7 and allow cross-domain resource sharing in your applications.
The above is the detailed content of How do I enable Cross-Origin Resource Sharing on IIS 7?. For more information, please follow other related articles on the PHP Chinese website!