Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept HTTP OPTIONS before windows authentication ASP.NET Core 2.0

how to intercept an http options request (Cors Preflight) before the windows authentication of IIS ?

I make a middleware and i tryed this :

 public async Task Invoke(HttpContext context)
    {
        if (context.Request.Method.Equals("OPTIONS"))
        {
            context.Response.StatusCode = 204;
            context.Response.Headers.Add("Access-Control-Allow-Origin", new [] {"http://cwfr003320:9393"});
            context.Response.Headers.Add("Access-Control-Allow-Credentials", new[] { "true" });
            context.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "GET, POST, PUT, DELETE, OPTIONS" });
            context.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "Accepts, Content-Type, Origin,Access-Control-Allow-Origin, Access-Control-Allow-Credentials" });
            context.Response.Headers.Add("Access-Control-Max-Age", new[] { "3600" });
            return; 
        }
        await _next.Invoke(context);
    }

But, i get a 401 code for the options request. How i can resolve this error ? With Anonymous authentication it's work correctly...

Thank you in advance.

like image 279
FRZ7 Avatar asked Nov 23 '25 19:11

FRZ7


1 Answers

You need to use IIS CORS module to allow preflight requests to go through Windows authentication,

https://blogs.iis.net/iisteam/getting-started-with-the-iis-cors-module

like image 107
Lex Li Avatar answered Nov 26 '25 11:11

Lex Li