未处理 OWIN 预检请求

OWIN preflight requests are not processed

从浏览器到我的 Self-Host OWIN WebAPI 的所有预检请求都不会被中间件处理。如果我向 Postman 发出 OPTIONS 请求,它们就会被处理。为什么会有这样的行为?

Request URL:http://localhost:9000/api/v1/conversations/create?connectionId=13509f44-eacb-4950-8cc8-71bd37098975

Request Method:OPTIONS

Status Code:401 Unauthorized Remote

Address:[::1]:9000

Accept:/

Accept-Encoding:gzip, deflate, sdch, br

Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4

Access-Control-Request-Headers:content-type

Access-Control-Request-Method:POST

Connection:keep-alive

Host:localhost:9000

Origin:http://localhost:8080

Referer:http://localhost:8080/

User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

对 Chrome 的响应 Headers:

Content-Length:0

Date:Wed, 08 Feb 2017 04:17:26 GMT

Server:Microsoft-HTTPAPI/2.0

WWW-Authenticate:NTLM

邮递员的回复headers:

Access-Control-Allow-Credentials →true

Access-Control-Allow-Origin →chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop

Allow →POST

Content-Length →76

Content-Type →application/json; charset=utf-8

Date →Wed, 08 Feb 2017 04:21:02 GMT

Server →Microsoft-HTTPAPI/2.0

我在我的应用程序构建器中添加了伪造的中间件:

public void BuildWebApi(IAppBuilder appBuilder)
    {
        appBuilder.Use(async (ctx, next) =>
        {
            await next();
        });

并将断点放在第 "await next()" 行。所以当浏览器发出预检请求时断点不会停止,而当邮递员选项响应时断点不会停止。

解决了这个问题
HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemeSelectorDelegate = request =>
{
   if (request.HttpMethod == "OPTIONS")
   {
       return AuthenticationSchemes.Anonymous;
   }
};