IAuthenticationFilter.OnAuthenticationChallenge()的目的是什么

Use*_*rol 7 .net authentication asp.net-mvc asp.net-mvc-5

我在注册的自定义IAuthenticationFilter实现RegisterGlobalFilters().在我的项目中,我正在目睹以下呼叫序列:

  1. IAuthenticationFilter.OnAuthentication
  2. 授权(如果有的话)
  3. 控制器动作
  4. IAuthenticationFilter.OnAuthenticationChallenge

为什么控制器动作之后会发生?从这篇博文中我读到了

要记住的关键是,OnAuthenticationChallenge不一定要在每个其他Action Filter之前运行.它可以在不同的阶段运行.

如果我们无法判断它究竟何时被调用,它怎么有用呢?

Dmi*_*ryK 8

资源

"只要请求失败了操作方法的身份验证或授权策略,MVC框架就会调用OnAuthenticationChallange方法.OnAuthenticationChallenge方法传递一个AuthenticationChallengeContext对象,该对象派生自ControllerContext类"

所以,一个实际的例子是:

1 - 您设置自定义授权过滤器

2 - 用户在授权方法上失败

3 - 调用OnAuthenticationChallenge方法.

  • OnAuthenticationChallenge被称为做什么? (9认同)