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

MySQL 8.X installation tutorial under Windows

I had been using MySQL 5.7 before, but because My...

Practical record of MySQL 5.6 master-slave error reporting

1. Problem symptoms Version: MySQL 5.6, using the...

React Principles Explained

Table of contents 1. setState() Description 1.1 U...

Use Angular CDK to implement a Service pop-up Toast component function

Table of contents 1. Environmental Installation 2...

MySQL index principle and query optimization detailed explanation

Table of contents 1. Introduction 1. What is an i...

WeChat applet implements a simple calculator

WeChat applet's simple calculator is for your...

10 Popular Windows Apps That Are Also Available on Linux

According to data analysis company Net Market Sha...

JavaScript implements single linked list process analysis

Preface: To store multiple elements, arrays are t...

Introduction to 10 Hooks in React

Table of contents What is ReactHook? React curren...

Solve the scroll-view line break problem of WeChat applet

Today, when I was writing a small program, I used...

MySQL Installer Community 5.7.16 installation detailed tutorial

This article records the detailed tutorial of MyS...

MySQL 5.7.15 version installation and configuration method graphic tutorial

This article shares with you a detailed tutorial ...