【问题标题】:CORS on asp.net core web api doesn't work after deploy on IIS在 IIS 上部署后,asp.net core web api 上的 CORS 不起作用
【发布时间】:2020-02-04 14:18:01
【问题描述】:

我正在开发一个带有角前端的网络核心 webapi。当我调试一切正常时,现在我已经在 IIS 上部署了 web abi,​​我无法让它工作,它给了我 CORS 错误:"...No 'Access-Control-Allow-Origin' header is present on the requested resource."

这是在 web api 上启用 CORS 的代码:

public void ConfigureServices(IServiceCollection services)
{
...
    services.AddCors();

...
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
    app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());

我尝试了在此站点和其他站点中找到的不同解决方案,但似乎没有任何效果

我应该在我的 Angular 应用程序中添加某种标题吗? 问题可能出在 IIS 上吗?

【问题讨论】:

  • 您是否检查了您实际收到的标头?在浏览器的控制台中查找其他错误?

标签: angular asp.net-core iis


【解决方案1】:

你可能想看看这个。

在您的 Startup.cs 文件中

// This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {
           services.AddCors(cfg =>
           {
                cfg.AddDefaultPolicy(policy =>
                {
                     policy.WithOrigins("list of origins to allow here")
                     .AllowAnyHeader()
                     .AllowAnyMethod()
                     .AllowCredentials()
                     .SetIsOriginAllowed((_) => true)
                     .SetIsOriginAllowedToAllowWildcardSubdomains();
                });
           });


services.AddMvc()
                .AddJsonOptions(options =>
                {
                     options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                     options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                     options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
                })
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
      }


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
      {
         app.UseCors();
         app.UseMvc(routes =>
           {
                routes.MapRoute(
                     name: "default",
                     template: "{controller=Home}/{action=Index}/{tag?}");
           });
      }

确保在调用 MVC 之前调用 Cors。

我希望这会有所帮助。

问候

【讨论】:

  • 我不使用 Mvc,我使用 services.AddControllers();app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); 如果我尝试使用你的代码,它会给我这个错误:'JsonOptions' 不包含'的定义SerializerSettings' 当然不行(只有在调试时才有效)
  • 在您的情况下只需摆脱 AddMvc 和 UseMvc。
  • 已经完成了,我复制了“服务”部分和“app.UseCors();”,仍然无法正常工作,就像我在第一条评论中写的那样
  • 好的,你能试试这个吗? docs.microsoft.com/en-us/aspnet/core/security/…
  • 我已经尝试了所有方法,似乎没有任何效果,我什至使用[AllowAnonymous] 创建了一个方法,但它给了我错误 400。可能存在一些 IIS 配置错误(我使用本指南:@987654322 @)
【解决方案2】:

我终于设法解决了这个问题: 我在 IIS 上启用了“基本身份验证”,因为我错误地认为这是正确的做法。

我发现 IIS“基本身份验证”与我实现的不同(我创建了一个自定义“BasicAuthenticationHandler”来读取标头并验证用户)

问题是 IIS(或 Angular,或 Chrome,我不知道)而不是返回错误“401 未授权”给了我 CORS 错误,即使启用了“匿名身份验证”(必须禁用所有其他身份验证) .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2019-10-17
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多