简体   繁体   English

对/ token的Web API请求有效,但对其他api控制器的请求则抛出404(与CORS相关)

[英]web api request to /token works but request to other api controllers throws 404 (cors related)

I am a little puzzled with issue here. 我对这里的问题有些不解。

I have enabled CORS by adding the following code in startup 我通过在启动时添加以下代码来启用CORS

var enableCorsAttribute = new EnableCorsAttribute("*",
                                           "Origin, Content-Type, Accept",
                                           "GET, PUT, POST, DELETE, OPTIONS");
        config.EnableCors(enableCorsAttribute);

And request to /token is working correctly. 并且对/ token的请求正常工作。 I get the correct token, and I have tested using postman. 我得到了正确的令牌,并且已经使用邮递员进行了测试。

When I try to request to other API controllers it fires 404 error saying 当我尝试请求其他API控制器时,它会触发404错误提示

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。

I understand that's the header is missing but I am not sure how to configure this other than the code I have posted above. 我知道缺少标题,但是除了上面发布的代码外,我不确定如何配置它。

I am running angular 2 app to perform the request. 我正在运行angular 2应用程序来执行请求。

Any help would be much appreciated! 任何帮助将非常感激!

I finally got it working after spending about 2 days... hopefully this answer helps other who might have the same problem. 经过大约两天的时间,我终于开始使用它了……希望这个答案可以帮助其他可能遇到相同问题的人。

One thing to note - if you implement or extend simple auth code, 需要注意的一件事-如果您实现或扩展简单的身份验证代码,

inside this method GrantResourceOwnerCredentials you will have the following line of the code. 在此方法GrantResourceOwnerCredentials您将具有以下代码行。

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });

This is causing the inconsistency between /token request headers and request to other API controllers. 这导致/ token请求标头与对其他API控制器的请求之间存在不一致。

Long story short, comment this line so same request header is applied. 长话短说,注释此行,以便应用相同的请求标头。

I have made changes to the web.config as the following 我对web.config进行了如下更改

 <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" />      
  <remove name="WebDav" />
  <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="IsapiModule" scriptProcessor="C:\Windows\System32\inetsrv\asp.dll" resourceType="Unspecified" requireAccess="None" />      
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />      
</handlers>
<httpProtocol>
  <customHeaders>

    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="*" />
    <add name="Access-Control-Allow-Methods" value="*" />
  </customHeaders>
</httpProtocol>

And just in case you still have problems with options request, you can go to global.asax.cs file and modify the response as the following: 并且以防万一您仍然对选项请求有疑问,可以转到global.asax.cs文件并按如下所示修改响应:

if ((HttpContext.Current.Request.HttpMethod == "OPTIONS"))
            { 

            HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
            }

I really hope this answer saves someone else's time! 我真的希望这个答案可以节省别人的时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM