【问题标题】:Response to preflight request doesn't pass access control (Angular 2 - Web Api)对预检请求的响应未通过访问控制(Angular 2 - Web Api)
【发布时间】:2018-04-29 15:00:39
【问题描述】:

我有几个项目,其中一个是用于身份验证的。但是我设置了CORS 配置,它仍然返回错误。

注意:Web Api 应用可与邮递员配合使用。

这是我到目前为止所做的:

Startup.cs

[assembly: OwinStartup(typeof(LabelGenerator.Startup))]
namespace LabelGenerator {
    public partial class Startup {
        public void Configuration(IAppBuilder app) {
            ConfigOAuth(app);
            ConfigWebApi(app);
        }
    }
}

Startup.Auth.cs

namespace LabelGenerator {
    public partial class Startup {
        private void ConfigOAuth(IAppBuilder app) {
            OAuthBearerAuthenticationOptions oAuthServerOptions = new OAuthBearerAuthenticationOptions();
            // Token Generation
            app.UseOAuthBearerAuthentication(oAuthServerOptions);
        }

        private void ConfigWebApi(IAppBuilder app) {
            HttpConfiguration conf = new HttpConfiguration();
            WebApiConfig.Register(conf);
            app.UseWebApi(conf);

            // CORS
            app.UseCors(CorsOptions.AllowAll);
        }
    }
}

Web.config

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

【问题讨论】:

    标签: c# angular cors asp.net-web-api2 http-status-code-405


    【解决方案1】:

    根据这个answer我改变了这个:

    private void ConfigWebApi(IAppBuilder app) {
        HttpConfiguration conf = new HttpConfiguration();
        WebApiConfig.Register(conf);
        app.UseWebApi(conf);
    
        // CORS
        app.UseCors(CorsOptions.AllowAll);
    }
    

    到这里:

    private void ConfigWebApi(IAppBuilder app) {
        HttpConfiguration conf = new HttpConfiguration();
        WebApiConfig.Register(conf);
    
        // CORS
        app.UseCors(CorsOptions.AllowAll);
        app.UseWebApi(conf);
    }
    

    现在CORS 工作正常。

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 1970-01-01
      • 2017-11-01
      • 2020-08-14
      • 2020-10-15
      • 2016-06-05
      相关资源
      最近更新 更多