【问题标题】:Web api Cross-Origin Request Blocked error when called Token service. Other services works调用 Token 服务时出现 Web api Cross-Origin Request Blocked 错误。其他服务工作
【发布时间】:2019-01-23 01:00:12
【问题描述】:

我有一个单独的 web api 项目和一个单独的简单 UI 项目。要在 UI 项目中访问 web api,我在 WebApiConfig.cs 中启用了CORS,如下所示

    var cors = new EnableCorsAttribute("http://localhost:49567", "*", "*","*");
    config.EnableCors(cors);

在 AccountController.cs 中,当 [Authorize] 被禁用时,我可以使用 ajax 从 UI html 项目页面访问 API。

$.ajax({
        type: "GET",
        url: "http://localhost:51401/api/Account/UserInfo",
        data: "",
        contentType: "application/json; charset=utf-8",
        success: VerifyResponse,
        dataType: "json",
        crossDomain: true,
        failure: ajaxCallFailed});

但是当我想通过首先调用/token 来启用[Authorize] 来创建令牌身份验证时,错误Cross-Origin Request Blocked: 在html 中重复。

$.ajax({
    method: "POST",
    url: "http://localhost:51401/Token",
    data: {
        username: $('#username').val(),
        password: $('#password').val(),
        grant_type: 'password'
    },
    contentType: "application/json",
    success: VerifyResponse,
    dataType: "json",
    crossDomain: true,
    failure: ajaxCallFailed
    });     

在@arista_14 建议后进行编辑后,我现在在 web api 项目中的 web.config

<system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>

    <validation validateIntegratedModeConfiguration="false" />
  <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
<httpProtocol>
   <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
       <add name="Access-Control-Allow-Headers" value="Content-Type" />
       <add name="Access-Control-Allow-Credentials" value="true" />
       <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
   </customHeaders>
</httpProtocol> 
  </system.webServer>

现在的错误是:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:51401/Token. (Reason: CORS preflight channel did not succeed).[Learn More]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:51401/Token. (Reason: CORS request did not succeed).[Learn More]

【问题讨论】:

    标签: c# asp.net-web-api


    【解决方案1】:

    我遇到了同样的问题。只需将此代码放在 web.config 中。它对我有用 -

    <httpProtocol>
       <customHeaders>
           <add name="Access-Control-Allow-Origin" value="*" />
           <add name="Access-Control-Allow-Headers" value="Content-Type" />
           <add name="Access-Control-Allow-Credentials" value="true" />
           <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
       </customHeaders>
    </httpProtocol>
    

    【讨论】:

    • 那么 WebApiconfig.cs 和 web.config 中的 enableconfig 都需要是吗?
    • 在 web.config 文件中。尝试任何一个。就我而言,此解决方案有效
    • @arista_14 通过更改修改了配置,但还没有运气。但是错误消息略有变化。通过更改修改了我的问题。 ajax调用中还有什么需要提到的吗?
    【解决方案2】:

    发生跨域阻塞的原因是端口不同。您在 CORS 中指定了此网址:http://localhost:49567

    然后继续使用以下网址调用它:http://localhost:51401

    注意端口的区别。在我将本地主机端口设置为不会随心所欲地改变的东西之前,我实际上遇到了同样的问题。

    我也做了另一个答案显示的配置,但没有用。

    【讨论】:

    • 我确实有 2 个单独的网址(2 个单独的项目一个 web api 和一个网站)。这就是启用 cors 的原因吧?
    • 尝试 var cors = new EnableCorsAttribute("", "", "","");
    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    相关资源
    最近更新 更多