访问多个Web API中的跨源问题

时间:2018-12-04 12:19:50

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

我正在做一个项目,要求我从一个有角度的6前端应用程序访问四(4)个Web Api。有一个专用于用户身份验证的api,另一个用于客户详细信息,另一个用于供应商详细信息及其出价,最后一个api用于项目监视。我已经在所有4个API上实现了cors,但是除了身份验证api以外,其他API均以cros错误响应。这是错误。

enter image description here 这是我遇到的错误

   <handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <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" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="POST,GET,PUT,PATCH,DELETE,OPTIONS" />
    <add name="Access-Control-Allow-Credentials" value="true" />
    <add name="Access-Control-Allow-Headers" value="Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name" />
  </customHeaders>
</httpProtocol>

以上配置已在所有4个API的web.config中注册。

我尝试像这样在WebAppConfig.cs上配置它

 public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors();

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

我有相同的cros错误,然后我尝试实施ICorsPolicyProvider策略,但仍然有相同的错误

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false)]
public class CustomCorsPolicyAttribute : Attribute, ICorsPolicyProvider
{
    CorsPolicy policy;  

   public  CustomCorsPolicyAttribute()
    {
        policy = new CorsPolicy
        {
            AllowAnyHeader = true,
            AllowAnyMethod = true
        };
        var allowedOrigins = ConfigurationManager.AppSettings["AllowedOrigins"].Split(',');
        foreach(var item in allowedOrigins)
        {
            policy.Origins.Add(item);
        }


    }


    public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return Task.FromResult(policy);
    }
}

请让我告诉我可能是什么问题和解决方案。非常感谢您的协助。

0 个答案:

没有答案