首页 > 解决方案 > 删除和选项请求获得 401 未经授权的响应

问题描述

我正在使用托管在远程 IIS 服务器上的 .net core web api 3.1,每次删除/选项请求我都会收到 401 未经授权的html响应,我在启动时允许了 CORS,但没有运气。

更新:这是我的启动

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<BrmajaCommerceSearchContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SearchConnectionString")));
        services.AddDbContext<IdentityContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        services.AddCors(o => o.AddPolicy("AllowAll", b => b.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
        services.AddControllers().AddNewtonsoftJson(s =>
        {
            s.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        });

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo
            {
                Title = "Ecommerce API",
                Version = "v1"
            });
            // Set the comments path for the Swagger JSON and UI.
            var xmlFile = Assembly.GetExecutingAssembly().GetName().Name + ".xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);
        });

        services.AddAuthentication()
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }


        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();
        app.UseCors("AllowAll");

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("swagger/v1/swagger.json", "Ecommerce");
            c.RoutePrefix = string.Empty;
        });
    }
}

标签: c#asp.net-coreiiscorsasp.net-web-api2

解决方案


描述行为的最可能原因是 IIS WebDAV 模块为这些 HTTP 动词提供服务。您必须使用 IIS 配置管理器或使用包含的 web.config 文件禁用它

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <modules>
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <remove name="WebDAV" />
    </handlers>
  </system.webServer>
<configuration>

推荐阅读