Angular 具有Windows身份验证的.Net核心WebAPI CORS

Angular 具有Windows身份验证的.Net核心WebAPI CORS,angular,asp.net-web-api,asp.net-core-webapi,Angular,Asp.net Web Api,Asp.net Core Webapi,我有一个.Net Core WebAPI服务,我为其启用了CORS(代码如下),在项目的属性中,我禁用了匿名身份验证并启用了Windows身份验证。POST和PUT端点在启用匿名身份验证的情况下工作,但在禁用匿名身份验证时失败。我明白了 OPTIONS http://localhost:64113/api/ 401 (Unauthorized) 启用CORS的代码 services.AddCors(options => { opti

我有一个.Net Core WebAPI服务,我为其启用了CORS(代码如下),在项目的属性中,我禁用了匿名身份验证并启用了Windows身份验证。POST和PUT端点在启用匿名身份验证的情况下工作,但在禁用匿名身份验证时失败。我明白了

OPTIONS http://localhost:64113/api/ 401 (Unauthorized)
启用CORS的代码

        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.WithOrigins("http://localhost:3000")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
        });
角码

    public XXX(data: any): Observable<Response> {
    return this.http.put(this.baseUrl, data,
        { withCredentials: true });
}
public XXX(数据:任意):可观察{
返回this.http.put(this.baseUrl,data,
{withCredentials:true});
}
有人有过这方面的经验吗


谢谢,我也有同样的问题。终于找到了适合我的解决方案。因此,您可以尝试遵循以下模式:

  • 通过执行以下操作启用CORS中间件(您已经完成):

    services.AddCors(options ={
      ...
      //describe your options here
      ...    
    });
    
  • 为IIS/IIS Express启用Windows身份验证和匿名身份验证(取决于您使用的内容)

  • 使用
    forwardWindowsAuthToken=“true”
    标志将web.config添加到项目的根文件夹中。在我的示例中,它如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.webServer>
       <handlers>
         <remove name="aspNetCore"/>
         <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
       </handlers>
       <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true"/>
      </system.webServer>
    </configuration>  
    
    
    
  • [Authorize]
    属性应用于控制器/操作。就这样。现在,只需访问
    user.identity.Name
    属性,您就可以发送POST&PUT请求并获取用户身份