Asp.net Web API项目的表单身份验证和OAuth混合使用不';如果令牌过期,则返回401
我正在开发Asp.net Web API项目的表单身份验证和OAuth混合使用不';如果令牌过期,则返回401,asp.net,asp.net-mvc,oauth,asp.net-web-api2,forms-authentication,Asp.net,Asp.net Mvc,Oauth,Asp.net Web Api2,Forms Authentication,我正在开发ASP.netwebapi应用程序,它使用OAuth(owin)协议对我的客户端进行身份验证。在API的初始版本发布一段时间后,我添加了额外的表单身份验证(基于cookie),并在其上加入了ASP.NET MVCapp。现在,在启动期间,我 // forms app.UseCookieAuthentication(... options ...); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie
ASP.netwebapi
应用程序,它使用OAuth
(owin
)协议对我的客户端进行身份验证。在API的初始版本发布一段时间后,我添加了额外的表单身份验证(基于cookie),并在其上加入了ASP.NET MVC
app。现在,在启动期间,我
// forms
app.UseCookieAuthentication(... options ...);
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// oauth
app.UseOAuthAuthorizationServer
它工作得非常完美,我的MVC用户可以调用我的API,而无需进行身份验证,这很酷。唯一的缺点是,当我的API客户端尝试使用过期的令牌而不是401错误访问API时,我的登录页面会收到200和html响应,这导致了很多问题
Request:
GET https://server.com/api/v1/controller/action1
Authoration: bearer <expired token here>
Response:
200 <html>
请求:
得到https://server.com/api/v1/controller/action1
作者:持证人
答复:
200
我需要的是,任何带有过期令牌的API请求都应该导致403HTTP响应,并且没有登录页面
Request:
GET https://server.com/api/v1/controller/action1
Authoration: bearer <expired token here>
Response:
200 <html>
是否可以在混合环境中实现,还是应该拆分解决方案?
CookieAuthenticationProvider
具有自定义此行为的处理程序
Provider = new CookieAuthenticationProvider()
{
OnApplyRedirect = ctx =>
{
// check if ctx is an API request
if (!IsApiRequest(ctx))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}