【问题标题】:Acess denied / Method Not allowed - some browsers only访问被拒绝/方法不允许 - 仅限某些浏览器
【发布时间】:2017-05-04 11:44:47
【问题描述】:

我创建了一个 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”删除行是在查看其他帖子后添加的,但似乎没有任何区别。 我还尝试按照here 的描述添加一个新的“global.asax”,但这也没有帮助。

【问题讨论】:

    标签: ajax wcf


    【解决方案1】:

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

    [WebInvoke(Method = "*"
    

    这样您就可以让您的方法接收选项请求。

    为非 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找到。

    【讨论】:

    • 我试过了,但没有成功;无论如何都感谢您的关注
    • 亲爱的@quilkin。你知道可选请求中会发生什么吗?请确保您的服务不会在可选请求中抛出异常(返回 200)。为了检查这一点,您可以使用 Fiddler4。只需将方法更改为可选调用您的 Web 服务。其他一切看起来都不错。希望这能解决您的问题。
    • 我不知道你所说的“可选请求”是什么意思——我在谷歌上搜索过,但没有找到任何有用的结果。安装 Fiddler4;这表明没有任何用处,但也许我不知道如何最好地使用它。刚刚查看了服务器日志,发现从 IE10(和 Android chrome)调用 Login 方法的响应为 400,而来自 Firefox 的调用响应为 200。 400 听起来像错误的 json 格式,但为什么不同浏览器会有所不同呢?
    • 我亲爱的@quilkin。我的意思是可选请求:此方法允许客户端确定与资源相关联的选项和/或要求,或服务器的功能,而无需暗示资源操作或启动资源检索。我还编辑了我的答案。
    • 为非 GET 请求启用 CORS 不仅需要设置 Access-Control-Allow-Origin 标头 - 它还需要处理预检请求,即询问服务器是否安全的 OPTIONS 请求在发送实际请求之前执行可能会更改数据的操作(例如 POST、PUT、DELETE)。
    【解决方案2】:

    我终于找到了解决方案(或 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 2021-06-30
      • 2012-06-05
      • 2015-03-26
      相关资源
      最近更新 更多