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

Detailed explanation of the basic usage of VUE watch listener

Table of contents 1. The following code is a simp...

How to implement mask layer in HTML How to use mask layer in HTML

Using mask layers in web pages can prevent repeat...

Mybatis statistics of the execution time of each SQL statement

background I am often asked about database transa...

How to make JavaScript sleep or wait

Table of contents Overview Checking setTimeout() ...

Implementation of master-slave replication in docker compose deployment

Table of contents Configuration parsing Service C...

Detailed explanation of the life cycle of Angular components (Part 2)

Table of contents 1. View hook 1. Things to note ...

Implementation of 2D and 3D transformation in CSS3

CSS3 implements 2D plane transformation and visua...

jQuery implements all shopping cart functions

Table of contents 1. Select All 2. Increase or de...

Summary of MySQL development standards and usage skills

1. Naming conventions 1. Database names, table na...

Summary of Nginx location and proxy_pass path configuration issues

Table of contents 1. Basic configuration of Nginx...