javascript - 如何使用 Windows 身份验证在 IIS 上授权 CORS 预检请求

标签 javascript asp.net-web-api cors windows-authentication preflight

我有一个关于 ASP.net Core 2(Windows 身份验证)的 API 和一个关于 Angular 的前端。
我进行了 cors 配置以从 SPA Angular 查询我的后端,但由于预检而被 IIS 服务器拒绝,因为他没有识别信息而被阻止。

错误信息 :

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://XXXXXX' is therefore not allowed access. The response had HTTP status code 401.

代码端正面:
//MY HEADER
private headers = new Headers({
    'Content-Type': 'application/json', 
    'Access-Control-Allow-Credentials':'true',
    'Access-Control-Allow-Origin':'true'
});

//REQUEST
let options = new RequestOptions({headers:this.headers, withCredentials:true});
return this.http.get(this.tasksUrl,options).map(res=>res.json());

代码背面:(Startup.cs)
public void ConfigureServices(IServiceCollection services)
{
   services.AddCors();
   services.AddCors(options =>
   {
       options.AddPolicy("AllowSpecificOrigin",
            builder =>
            {
               builder.WithOrigins("http://theURLofTheFront:8080" )
               .AllowAnyMethod()
               .AllowAnyHeader()
               .AllowCredentials();
            });
   });
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        app.UseCors("AllowSpecificOrigin");
        app.UseMvc();
    }

我试试这个:

CORS preflight request returning HTTP 401 with windows authentication .

我添加了自定义标题以在 IIS 上指定“访问控制允许来源”,不适合我。

这对我不起作用:
https://blogs.msdn.microsoft.com/friis/2017/11/24/putting-it-all-together-cors-tutorial/

我无法删除默认授权规则。

我提前感谢你

最佳答案

有几种方法可以做到这一点,其他答案可以在这个类似的问题上找到 --> Angular4 ASP.NET Core 1.2 Windows Authentication CORS for PUT and POST Gives 401

CORS 模块
可以使用 CORS Module 配置 IIS .
如此处所示:https://blogs.iis.net/iisteam/getting-started-with-the-iis-cors-module
更多信息请点击此处:https://blogs.iis.net/iisteam/getting-started-with-the-iis-cors-module

The IIS CORS module is designed to handle the CORS preflight requests before other IIS modules handle the same request. The OPTIONS requests are always anonymous, so CORS module provides IIS servers a way to correctly respond to the preflight request even if anonymous authentification needs to be disabled server-wise.


您需要通过 Webconfig 启用 CORS 模块:
<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <cors enabled="true">
      <add origin="*" allowCredentials="true" />
    </cors>
  </system.webServer>
</configuration>
更精细的控制:
<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <cors enabled="true">
      <add origin="https://readonlyservice.constoso.com" allowCredentials="true">
        <allowMethods>
            <add method="GET" />
            <add method="HEAD" />
        </allowMethods>
        <allowHeaders>
            <add header="content-type" /> 
            <add header="accept" /> 
        </allowHeaders>
      </add>
      <add origin="https://readwriteservice.constoso.com" allowCredentials="true">
        <allowMethods>
            <add method="GET" />
            <add method="HEAD" />
            <add method="POST" />
            <add method="PUT" /> 
            <add method="DELETE" />         
        </allowMethods>
      </add>
    </cors>
  </system.webServer>
</configuration>

重定向选项
您可以重定向所有 OPTIONS 请求以始终提供 OK 状态。
然而,这将颠覆预检请求的整个想法,因此只有在适用于您的情况时才使用它。
安装 redirect module在 IIS 中。
将以下重定向添加到您的 Webconfig。
<rewrite>
    <rules>
        <rule name="CORS Preflight Anonymous Authentication" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{REQUEST_METHOD}" pattern="^OPTIONS$" />
            </conditions>
            <action type="CustomResponse" statusCode="200" statusReason="Preflight" statusDescription="Preflight" />
        </rule>
    </rules>
</rewrite>

中间件
或者,可以通过在 IIS 中启用匿名身份验证并在 Net Core API 中创建一个中间件来检查一个人是否经过正确身份验证,从而实现所需的结果。
中间件:
public AuthorizationMiddleware(RequestDelegate next, ILogger logger)
{
    _next = next;
    _log = logger;
}

public async Task Invoke(HttpContext httpContext)
{
    //Allow OPTIONS requests to be anonymous
    if (httpContext.Request.Method != "OPTIONS" && !httpContext.User.Identity.IsAuthenticated)
    {
        httpContext.Response.StatusCode = 401;
        await httpContext.Response.WriteAsync("Not Authenticated");
    }
    await _next(httpContext);
}

关于javascript - 如何使用 Windows 身份验证在 IIS 上授权 CORS 预检请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49450854/

相关文章:

javascript - 使用 process.stdin 在 Node.js 中接受来自终端的动态输入时出现问题

android - 将视频从 asp.net core web API 流式传输到 Android/iOS

asp.net - 防止 FormsAuthenticationModule 拦截 ASP.NET Web API 响应

laravel - 本地主机上的访问控制允许来源(Laravel 和 Vue)CORS CORB 错误

javascript - jQuery 和 jqGrid - For ...每个循环提取数据

javascript - 使用socket.io保存网页状态

security - Paypal 和微软 WebAPI : best security approach

javascript - 为什么koa-router不能放在koa-cors之前?

javascript - CORS 与 pysimplesoap 服务器

javascript - 用于年龄验证的正则表达式,仅使用 Javascript 接受 0-200 之间的年龄