ASP.NET Core 3.1 + CORS + Windows身份验证= 401未经授权

问题描述 投票:0回答:1

我已经在IIS 10上托管的.NET Core 3.1(.201)Web API中配置了IIS CORS Module,现在我在Windows身份验证用户的GET(不是选项)请求上获得了401 Unauthorizedwith or withoud] >控制器或操作级别的[Authorize]属性。 Swagger却执行了相同的操作,返回200,因此我认为授权有效,因此问题一定与CORS有关。

[我已经看到了一些变通方法,可以在Startup.cs中启用匿名身份验证并设置一些授权过滤器,但我想坚持使用IIS CORS模块,因为它旨在适当地涵盖此情况,以及其他。

所以也许有人可以帮助我弄清楚我在做什么错。

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <authentication mode="Windows" />
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <cors enabled="true" failUnlistedOrigins="false">
        <add origin="http://localhost:4300" allowCredentials="true">
          <allowHeaders allowAllRequestedHeaders="true">
            <add header="credentials" />
          </allowHeaders>
        </add>
      </cors>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="...\bin\Debug\netcoreapp3.1\MyProj.Api.exe" hostingModel="InProcess" forwardWindowsAuthToken="true">
        <environmentVariables>
          <environmentVariable name="COMPLUS_ForceENC" value="1" />
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

Startup.cs

services.AddAuthentication(IISDefaults.AuthenticationScheme);

...

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseStaticFiles();

app.UseEndpoints(endpoints => ...

JavaScript请求(使用fetch API)设置了credentials: include标头。

我也在他的优质网络日志上尝试了the approach suggested by Rick Strahl,但得到了相同的结果:401

[我已经在IIS 10上托管的.NET Core 3.1(.201)Web API中配置了IIS CORS模块,现在,无论Windows身份验证的用户,无论是否使用GET(非选项)请求,我都会收到401未经授权...] >

cors authorization windows-authentication asp.net-core-3.1 iis-10
1个回答
0
投票

您是否尝试过在Startup类中添加CORS?

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder.AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader());
    });

    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.