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

Take you to understand MySQL character set settings in 5 minutes

Table of contents 1. Content Overview 2. Concepts...

Recommend some useful learning materials for newbies in web design

Many people also asked me what books I read when ...

Detailed explanation of the execution process of JavaScript engine V8

Table of contents 1. V8 Source 2. V8 Service Targ...

WeChat applet learning notes: page configuration and routing

I have been studying and reviewing the developmen...

A summary of the reasons why Mysql does not use date field index

Table of contents background explore Summarize ba...

Vue implements countdown function

This article example shares the specific code of ...

Docker installs mysql and solves the Chinese garbled problem

Table of contents 1. Pull the mysql image 2. Chec...

Case analysis of several MySQL update operations

Table of contents Case Study Update account balan...

A link refresh page and js refresh page usage examples

1. How to use the link: Copy code The code is as f...

Secondary encapsulation of element el-table table (with table height adaptation)

Preface During my internship at the company, I us...

Let's talk about destructuring in JS ES6

Overview es6 adds a new way to get specified elem...

Let's talk about the Vue life cycle in detail

Table of contents Preface 1. Life cycle in Vue2 I...

MySQL-group-replication configuration steps (recommended)

MySQL-Group-Replication is a new feature develope...

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

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

Select does not support double click dbclick event

XML/HTML CodeCopy content to clipboard < div c...