【问题标题】:AuthenticateRequest event验证请求事件
【发布时间】:2010-10-26 21:41:55
【问题描述】:


Q 1. 据我了解,FormsAuthenticationModule 订阅了AuthenticateRequest 事件,因此只有在触发此事件后,才会调用FormsAuthenticationModule。但是下面的引用让我有点困惑:

  1. AuthenticateRequest 事件表明配置的身份验证机制已对当前请求进行身份验证。

    • 上面的引用不是表明当AuthenticateRequest 事件引发时,请求(又名用户)已经通过身份验证了吗?
  2. 订阅AuthenticateRequest 事件可确保在处理附加的模块或事件处理程序之前对请求进行身份验证。

    • 据我了解这句话,如果我们订阅AuthenticatedRequest,那么我们的事件处理程序将在FormsAuthenticationModule 之前被调用?因此Application_AuthenticateRequest() 会在FormsAuthenticationModule 被调用之前被调用?


Q 2. 我正在学习的书建议在Application_AuthenticateRequest() 中我们能够验证用户是否是特定角色的成员,如果不是,我们可以自动添加用户:

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
            if (User.Identity.IsAuthenticated && Roles.Enabled)
            {

                //here we can subscribe user to a role via Roles.AddUserToRole()
            }       
    }

从上面的代码来看,Application_AuthenticateRequest()FormsAuthenticationModule 被调用之后被调用,但是同一本书的其他地方暗示Application_AuthenticateRequest()FormsAuthenticationModule 之前被调用:

Application_AuthenticateRequest 在执行身份验证之前被调用。 这是创建您自己的身份验证逻辑的起点。


我错过了什么?


感谢

【问题讨论】:

    标签: c# asp.net authentication forms-authentication httpapplication


    【解决方案1】:

    似乎是首先处理 FormsAuthenticationModule。这个模块通常比 ASP.NET 管道中的任何自定义模块早,所以当 AuthenticateRequest 被触发时,FormsAuthenticationModule 将首先被调用,完成它的工作,然后你的模块的事件处理程序将被调用。

    如果您真的想深入研究,我建议您尝试自己调试 ASP.NET 代码。这是一篇如何设置 VS 的帖子:

    http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx

    编辑:我能够通过在 Global.asax 中使用自定义模块和事件处理程序设置一个 Web 项目来确认此行为。看一下HttpApplication.InitInternal的源码,初始化顺序如下:

    • 集成模块的初始化:FormsAuthenticationModule 连接到 HttpApplication.AuthenticateRequest 事件
    • 自定义模块的初始化:自定义模块与 HttpApplication.AuthenticateRequest 事件挂钩
    • 初始化 Global 类 (global.asax):这里我们连接到 AuthenticateRequest 事件
    • HttpApplication.InitInternal 按照特定名称模式(例如 Application_AuthenticateRequest)搜索 Global 类上的方法,将它们与事件匹配并连接

    初始化之后,当 AuthenticateRequest 触发时,事件处理程序会按照它们初始化的顺序被调用,所以:

    • FormsAuthenticationModule.AuthenticateRequest 事件处理程序
    • CustomModule.AuthenticateRequest 事件处理程序
    • Global.AuthenticateRequest 事件处理程序
    • Global.Application_AuthenticateRequest 方法

    除非我错过了什么,没有机制可以阻止事件处理程序触发,所以无论 FormsAuthenticationModule.AuthenticateRequest 的结果如何,下一个处理程序仍然会被调用。我希望这会有所帮助。

    【讨论】:

    • Q1 如果我理解正确,那么当 AuthenticateRequest 触发时,首先调用 FormsAuthenticationModule,然后是 Application_AuthenticateRequest(),然后才调用自定义身份验证模块? Q2 - 但是 MSDN 引用(“AuthenticateRequest 事件表明配置的身份验证机制已经验证了当前请求”),这意味着 AuthenticateRequest 仅在 FormsAuthenticationModule 完成其工作后才被触发?
    • 帖子中的详细答案。关于 Q2 - 我想这并不完全正确:“AuthenticateRequest 事件表明配置的身份验证机制已经对当前请求进行了身份验证” - 它当然通过了 FormsAuthenticationModule 上的事件处理程序,但我们不知道结果;)
    • 谢谢。你真的帮了我这个忙
    • 一点小提示:FormsAuthenticationModule.Authenticate 事件(不称为 AuthenticateRequest)被称为 [Application.AuthenticateRequest 事件](FormsAuthenticationModule)。您可以像注册任何其他事件一样注册该事件,使用 global.asax 中的预定义名称。
    • 非常感谢您的这篇文章。我希望 HttpApplication 类有一个接口,它明确定义了 Application_AuthenticateRequest() 方法。
    【解决方案2】:

    如果你想访问用户对象,我建议你使用

    protected void Application_Start()
    {
        PostAuthenticateRequest += Application_PostAuthenticateRequest;
    }
    
    protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        if(User.Identity.IsAuthenticated)
        {
            //Do stuff here
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-18
      • 2018-04-02
      • 2017-11-21
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多