从浏览器到我的自托管 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 的响应 header :
Content-Length:0
Date:Wed, 08 Feb 2017 04:17:26 GMT
Server:Microsoft-HTTPAPI/2.0
WWW-Authenticate:NTLM
Postman 的响应 header :
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()”行。因此,断点不会在浏览器发出预检请求时停止,而在 postman OPTIONS 响应时不会停止。
最佳答案
解决了这个问题
HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemeSelectorDelegate = request =>
{
if (request.HttpMethod == "OPTIONS")
{
return AuthenticationSchemes.Anonymous;
}
};
关于c# - 未处理 OWIN 预检请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42104716/