2017-02-08 56 views
1

从浏览器到我的自主主机OWIN WebAPI的所有预检请求均不由中间件处理。如果我从Postman发出OPTIONS请求,它们将被处理。为什么是这样的行为?未处理OWIN预检请求

请求 网址:http://localhost:9000/api/v1/conversations/create?connectionId=13509f44-eacb-4950-8cc8-71bd37098975

请求方法:OPTIONS

状态代码:401个未经授权远程

地址:[:: 1]:9000

接受:/

接受编码:gzip,放气,SDCH,BR

接受语言:RU-RU,如; Q = 0.8,的en-US; Q = 0.6,连接; Q = 0.4

访问控制 - 请求报头:内容类型

访问控制请求-方法:POST

连接:保持活跃

主机:本地主机:9000

来源:http://localhost:8080

的Referer:http://localhost:8080/

的User-Agent:Mozilla的/ 5.0(Windows NT的10.0; Win64平台; 64)为AppleWebKit/537.36(KHTML,例如Gecko)铬/ 56.0.2924.87 Safari浏览器/ 537.36

为Chrome响应头:

的Content-Length:0

日期:星期三,08 2017年2月4时17分26秒GMT

服务器:HTTPAPI/2.0

WWW验证:NTLM

为邮差响应头:

接入控制允许凭证→真

访问控制允许来源→铬扩展:// fhbjgbiflinjbdggehcddcbncdddomop

允许→POST

Content-Length→76

Content-Type→applicati上/ JSON;字符集= UTF-8

日期→星期三,2017年2月8日4时21分02秒GMT

服务器→微软HTTPAPI/2。0

我加了假中间件到我的AppBuilder:

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

,并把断点排队 “等待下一个()”。因此,当浏览器进行预检请求时,断点不停,而邮递员OPTIONS响应停止。 //计算器:

+0

http://stackoverflow.com/questions/24989769/cors-is-not-working-in-web-api-with-owin-authentication和HTTP解决了这个.com/questions/20079813/how-to-make-cors-authentication-in-webapi-2可能是相关的。如果你还在你的问题中包含了请求标题,它也可能会有所帮助。 – sideshowbarker

+0

UseCors无法正常工作,因为它在后台添加的中间件未从Chrome的预检请求中调用。当然,我添加了UserCors()和config.EnableCors(),但它没有效果。 – Argnist

+0

行,所以http://stackoverflow.com/questions/20079813/how-to-make-cors-authentication-in-webapi-2/25758949#25758949不帮你吗? – sideshowbarker

回答

3

通过

HttpListener listener = (HttpListener)appBuilder.Properties["System.Net.HttpListener"]; 
listener.AuthenticationSchemeSelectorDelegate = request => 
{ 
    if (request.HttpMethod == "OPTIONS") 
    { 
     return AuthenticationSchemes.Anonymous; 
    } 
};