Acess被拒绝/方法不允许 - 仅限某些浏览器

时间:2017-05-04 11:44:47

标签: ajax wcf

我已经创建了一个WCF服务,并使用ajax / json'get'命令与它进行通信。它在Edge,Chrome(Windows桌面),Firefox(Windows桌面)上运行良好。但是随着IE10的到来,我得到了

XMLHttpRequest: Network Error 0x80070005, Access is denied.

我的Android手机上有Chrome浏览器

error 405 (Method not allowed). 

手机上的Firefox也失败了(只报告'网页错误'),但我没有任何调试器,因此无法检查底层错误。 在iPad上(使用Safari),浏览器只是崩溃而没有消息。

首先访问的方法是“登录”,但如果我将其注释掉,请硬编码我的登录详细信息并尝试其他方法(GetLocations)我收到同样的错误。

网站是quilkin.co.uk,如果有人想看看的话。如果它正常工作,如果您尝试登录,则应该收到“无效用户”或“无效密码”。是的,我知道它不安全!我稍后会对此进行排序。

web.config如下(抱歉格式错误):

<?xml version="1.0"?>
<configuration>

<appSettings>
  <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
   <customErrors mode="Off"/>
   <compilation debug="true" targetFramework="4.5.2"/>
   <httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
  <behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https"/>
  </protocolMapping>    
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
 <handlers>
        <remove name="WebDAV"/>
     </handlers>
 <httpProtocol>
     <customHeaders>
      <clear />
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,Authorization" />
      <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, ORIGIN" />
     </customHeaders>
   </httpProtocol>
   <modules runAllManagedModulesForAllRequests="true">
        <remove name="WebDAVModule"/>
   </modules>

   <directoryBrowse enabled="true"/>
   <validation validateIntegratedModeConfiguration="false"/>
 </system.webServer>

</configuration>

“WebDAV”删除行是在查看其他帖子时添加的,但似乎没有任何区别。 我还尝试添加一个新的'global.asax',如here所述,但这也无济于事。

2 个答案:

答案 0 :(得分:0)

我猜我现在的问题。请在[ServiceContract]中使用*。像这样:

[WebInvoke(Method = "*"

这样您就可以使用方法接收选项请求。 example of options request in Fiddler4

  

为非GET请求启用CORS需要的不仅仅是设置   Access-Control-Allow-Origin标头 - 它还需要处理   预检请求,这是请求服务器的OPTIONS请求   执行可能发生变化的操作是否安全   在发送实际请求之前的数据(例如,POST,PUT,DELETE)。   我写了一篇关于为WCF添加CORS支持的博客文章。这不是最简单的实现,但希望帖子中的代码可以简单地复制/粘贴到您的项目中。该帖子位于http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors-support-in-wcf.aspx

答案 1 :(得分:0)

最后我找到了解决方案(或 a 解决方案)。 This thread's第二个答案是关键。基本上我必须为每个POST方法添加一个额外的(空)方法,以处理某些浏览器发送的OPTIONS请求,例如:

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "/Login", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Login Login(Login login);

    [OperationContract]
    [WebInvoke(Method = "OPTIONS", UriTemplate = "/Login", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Login LoginOptions(Login login);