How to set up cross-domain access in IIS web.config

How to set up cross-domain access in IIS web.config

Requirement: The page needs to display an image, but due to various reasons, the image is on server 2, but the program to be displayed is on server 1. This causes a cross-domain problem when displaying. The original idea is to directly write a program to obtain the image path in the background, then download the image, and then return the image address to server 1. However, due to the uncertainty of this cycle and the urgency of the project at this stage, it was abandoned. Switch to the second method and download the image to the server through js. . After looking for some information and communicating with others, I found that it could not be realized. Then I planned to use js+canvas to save it locally, but then I found that it seemed that it could only be saved locally. Then the problem returned to the starting point: Is it possible to directly introduce pictures when they already exist on the website? After consulting the information and carefully analyzing it, after testing and debugging, the problem of accurately implementing the introduction of pictures was solved.

Process: Cross-domain solution for image import

Result: Change the webconfig settings to solve the cross-domain problem

<?xml version="1.0" encoding="utf-8"?>
 
<!--
  For more information about how to configure ASP.NET applications, visit http://go.microsoft.com/fwlink/?LinkId=169433
  -->
 
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
  <customErrors mode="Off"/> Here you can also see the error log in the external network <webServices>  
       	  <protocols>  
          	<add name="HttpGet"/>  
          	<add name="HttpPost"/>  
        	</protocols>  
      	</webServices>
 
    </system.web>
  <system.webServer>  
    <httpProtocol>   
    <customHeaders>   
      <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>   
      <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type"/>   
      <add name="Access-Control-Allow-Origin" value="*, http://192.168.0.178:8068" />   
    </customHeaders>   
  </httpProtocol>   
  
  </system.webServer>

The main problem is the restriction of cross-domain access permission set as above.

In this way, the cross-domain problem can be solved when displaying pictures on server 1.

How to set cross-domain in IIS web.config

In website development, there is sometimes a need for cross-domain, so how does IIS set up cross-domain? We can configure it directly in the web.config file. The setting method is as follows:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
      <add name="Access-Control-Allow-Headers" value="x-requested-with"/>
      <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

By setting the above web.config, we can achieve cross-domain website.

Here is a sharing of the cross-domain IIS webconfig IIS setting method

Set Access-Control-Allow-Origin

Open IIS, find "HTTP Response Header" and click it.

You can see Add on the right, then add the following header

Access-Control-Allow-Headers: Content-Type, api_key, Authorization

Access-Control-Allow-Origin: *

as follows

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
<add name="Access-Control-Allow-Headers" value="x-requested-with,Content-Type"/>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>

This is the end of this article about how to set up cross-domain access in IIS web.config. For more information about IIS cross-domain access, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  Detailed explanation of HTML document types

>>:  Detailed explanation of the MySQL MVCC mechanism principle

Recommend

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

How to quickly install Nginx in Linux

Table of contents What is nginx 1. Download the r...

Detailed explanation of several solutions for JavaScript interruption requests

Table of contents 1 Promise Interrupt the call ch...

Several mistakes that JavaScript beginners often make

Table of contents Preface Confusing undefined and...

How to use flat style to design websites

The essence of a flat website structure is simpli...

Instructions for using the meta viewport tag (mobile browsing zoom control)

When OP opens a web page with the current firmwar...

Usage of mysql timestamp

Preface: Timestamp fields are often used in MySQL...

Vue implements small notepad function

This article example shares the specific code of ...

Rounding operation of datetime field in MySQL

Table of contents Preface 1. Background 2. Simula...

Vue multi-page configuration details

Table of contents 1. The difference between multi...

Example of how to import nginx logs into elasticsearch

The nginx logs are collected by filebeat and pass...

Image hover toggle button implemented with CSS3

Result:Implementation Code html <ul class=&quo...

Solution to MySQL service 1067 error: modify the mysql executable file path

Today I encountered the MySQL service 1067 error ...