【问题标题】:Delete and Options requests gets a 401 unathorized response删除和选项请求获得 401 未授权响应
【发布时间】:2021-01-04 02:26:41
【问题描述】:

我正在使用托管在远程 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;
        });
    }
}

【问题讨论】:

  • 您介意发布 Startup.cs 吗?你的意思是它在 CUR 请求时返回 200?
  • @MichaelWang 只有 GET 和 POST 返回 200 其余返回 401,我发布了启动
  • 如果应用使用身份验证/授权功能,请在UseRouting 和UseCors 之后、UseEndpoints 之前调用UseAuthentication 和UseAuthorization。检查here。
  • @MichaelWang 这是一个 IIS 问题,因为请求没有到达它被返回 401 html 响应的 IIS 阻止的 api
  • 我不明白。您可以在 UseAuthorization 之前尝试 moveup UseCors 作为我发布的链接,然后在 IIS 上重新启动您的服务。

标签: c# asp.net-core iis cors asp.net-web-api2


【解决方案1】:

描述行为的最可能原因是 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>

【讨论】:

  • 我用的是.net core 没有web.config
  • 如果您使用 IIS,您当然可以将 web.config 放入您的 Web 应用程序的根目录,IIS 将使用它。相信我:)
  • 我已经从 iis 服务器上卸载了 cors 功能,但没有成功。
  • @KasperRoma 我的回答与 CORS 无关。您必须以一种或另一种方式禁用 WebDAV 模块。如果您有权访问 IIS 配置管理器,则可以将其从“模块”页面中删除,或者您可以使用服务器管理器从通用 HTTP 功能中卸载 WebDAV 发布,或者您可以像我之前提到的那样简单地使用您的应用程序发布 web.config。
  • 对不起,我错过了,我的意思是我已经卸载了 WebDAV Publishing。
【解决方案2】:

通过启用 iis 服务器上的所有动词来解决 -> .NET 授权规则

【讨论】:

    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多