enabling cross-origin resource sharing on IIS7

后端 未结 10 2058
轻奢々
轻奢々 2020-11-22 15:22

I recently ran into with posting Javascript requests to another domain. By default XHR posting to other domains is not allowed.

Following the instructions from htt

10条回答
  •  清酒与你
    2020-11-22 15:48

    Elaborating from DavidG answer which is really near of what is required for a basic solution:

    • First, configure the OPTIONSVerbHandler to execute before .Net handlers.

      1. In IIS console, select "Handler Mappings" (either on server level or site level; beware that on site level it will redefine all the handlers for your site and ignore any change done on server level after that; and of course on server level, this could break other sites if they need their own handling of options verb).
      2. In Action pane, select "View ordered list..." Seek OPTIONSVerbHandler, and move it up (lots of clicks...).

      You can also do this in web.config by redefining all handlers under ( then them back, this is what does the IIS console for you) (By the way, there is no need to ask for "read" permission on this handler.)

    • Second, configure custom http headers for your cors needs, such as:

      
        
          
            
            
            
          
        
      
      

      You can also do this in IIS console.

    This is a basic solution since it will send cors headers even on request which does not requires it. But with WCF, it looks like being the simpliest one.

    With MVC or webapi, we could instead handle OPTIONS verb and cors headers by code (either "manually" or with built-in support available in latest version of webapi).

提交回复
热议问题