CORS 405方法不允许

问题描述 投票:11回答:3

我一直关注Mozilla article如何设置我的网站以允许跨站点脚本请求。使用IIS管理器我添加了以下HTTP响应标头

Access-Control-Allow-Origin  : *
Access-Control-Allow-Headers : Origin, SecurityPrivateKeyID
Access-Control-Allow-Methods : GET, POST, PUT, DELETE, OPTIONS

尽管如此,当我的浏览器(Firefox和Chrome)使用自定义405 Method Not Allowed标头发送飞行前请求时,我仍然会收到SecurityPrivateKeyID

请求

OPTIONS /Service/Json/User.svc/ HTTP/1.1
Host: serviceprovider.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://client.com
Access-Control-Request-Method: GET
Access-Control-Request-Headers: securityprivatekeyid
Connection: keep-alive

响应

HTTP/1.1 405 Method Not Allowed
Allow: GET
Content-Length: 1565
Content-Type: text/html; charset=UTF-8
Server: Microsoft-IIS/8.0
access-control-allow-origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: Origin, SecurityPrivateKeyID
Date: Sat, 23 Mar 2013 08:35:03 GMT

直接访问http://serviceprovider.com/Service/Json/User.svc/时,该服务工作正常。

关于我做错了什么的任何想法?

[注意我已将主机文件更改为指向我的机器上的client.com和serviceprovider.com]

[使用JSONP的解决方案不会这样做,因为我的Web服务必须能够使用POST,PUT和DELETE方法]

cors iis-8
3个回答
21
投票

我的IIS 8实例是全新安装,似乎我需要对Handler Mappings进行一些修改

备份IIS配置

如果任何建议的更改破坏了您现有的网站,则最好备份applicationhost.config文件

  1. 导航到C:\Windows\System32\inetsrv\config
  2. 制作一份applicationhost.config

删除未使用的处理程序

作为起点,我删除了所有未使用的Handler映射以减少问题空间。您可以通过直接修改applicationhost.config或使用IIS管理器来完成此操作

  1. 打开IIS管理器
  2. 在服务器节点或单个网站节点上选择“处理程序映射”功能
  3. 手动删除不需要的所有映射。

我的网站基于服务,只依赖于静态文件和.aspx.svc文件扩展名的文件。我还在整个配置文件中手动删除了对.NET 2.0的所有引用。

添加OPTIONS处理程序

这似乎是修复。

  1. 打开IIS管理器
  2. 在服务器节点或单个网站节点上选择“处理程序映射”功能
  3. 在左侧栏中选择qazxsw poi
  4. Add Module Mapping对话框中,使用以下值。 Add Module Mapping - Request path * - Module ProtocolSupportModule - [留空] Executable - [无论你想要什么]
  5. 点击qazxsw poi 在qazxsw poi标签中,取消选中qazxsw poi 在Name标签中,确保选择Request RestrictionsMapping标签中选择Invoke handler only if request is mapped to

我生成的Handlers配置如下所示

Verbs

1
投票

对于我的实例:

  1. 确保请求中的“Access-Control-Allow-Headers”和“Access-Control-Allow-Methods”小于或等于响应中的值。(不要使用“*”)
  2. 在Web.config中删除此行OPTIONS

0
投票

在我的情况下,我不得不去Access,切换到Script,然后将<handlers accessPolicy="Read, Script"> <add name="OPTIONS" path="*" verb="OPTIONS" modules="ProtocolSupportModule" resourceType="Unspecified" /> <add name="svc-Integrated-4.0" path="*.svc" verb="*" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" /> <add name="SecurityCertificate" path="*.cer" verb="GET,HEAD,POST" modules="IsapiModule" scriptProcessor="%windir%\system32\inetsrv\asp.dll" resourceType="File" /> <add name="ISAPI-dll" path="*.dll" verb="*" modules="IsapiModule" resourceType="File" requireAccess="Execute" allowPathInfo="true" /> <add name="PageHandlerFactory-Integrated-4.0" path="*.aspx" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" /> <add name="CGI-exe" path="*.exe" verb="*" modules="CgiModule" resourceType="File" requireAccess="Execute" allowPathInfo="true" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" /> <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" /> </handlers> 一直移动到列表的顶部。

© www.soinside.com 2019 - 2024. All rights reserved.