PageMethods不会触发Application_PreRequestHandlerExecute事件。我该怎么用呢?

| 本文说明,无论出于何种原因,对于PageMethod调用,PreRequestHandlerExecute事件均不会触发。但是,我正在尝试使用该事件用用户的权限填充Principal对象,以便可以在任何Web请求中(无论是否调用PageMethod)对其进行检查。我正在缓存Session中的权限,因此我需要一个在调用PageMethod时触发的事件,并且需要访问Session。这样,我可以使用在会话中缓存的安全权限填充Principal对象,并且
User.IsInRole()
调用将按预期工作。我可以使用什么活动?     
已邀请:
        您应该实现一个授权模块,该模块将与到达服务器的每个请求一起运行。这样,您就可以授权委托人处理服务器提出的任何请求(页面请求,方法等)。
public class AuthorizationModule : IHttpModule, IRequiresSessionState
{
    //not going to implement it fully, might not compile

    public void Init( HttpApplication context )
    {
       //you\'ll prolly want to hook up to the acquire request state event, but read up to make sure this is the one you want on the msdn
       context.AcquireRequestState += AuthorizeRequest;
    }

    public void AuthorizeRequest( HttpContextBase httpContext )
    {
       // do you work in here

       // you can redirect them wherever if they don\'t have permssion, log them out, etc
    }
  }
}
创建完模块后,您需要将其连接到web.config中。您的类型应包含名称空间(如果有的话)。
<httpModules>
  <add name=\"AuthorizationModule\" type=\"AuthorizationModule\"/>
</httpModules>
我希望这有帮助。     
        您可以使用Application_OnPostAuthenticateRequest,如下所示(假设您使用的是表单身份验证。否则,请用身份验证机制替换代码):
public void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
{

    IPrincipal usr = HttpContext.Current.User;

    if (usr.Identity.IsAuthenticated && usr.Identity.AuthenticationType == \"Forms\")
    {
        var fIdent = (FormsIdentity)usr.Identity;
        var ci = new CustomIdentity(fIdent.Ticket);
        var p = new CustomPrincipal(ci);

        HttpContext.Current.User = p;
        Thread.CurrentPrincipal = p;
    }

}
    
        页面方法是静态的,并绕过正常的页面生命周期,其对象和事件。最好的办法是将身份验证信息作为参数传递给Page方法本身。     
        从我的角度来看,您可以: 1.-使用可以从具有访问Session变量权限的每个页面方法服务器代码中调用的通用方法。请参阅:  http://mattberseth.com/blog/2007/06/aspnet_ajax_use_pagemethods_pr.html 2.-稍后尝试使用__doPostBack()函数捕获服务器时执行类似的操作。看看这是否适合您捕获页面方法异步posbacks:  http://www.dotnetcurry.com/ShowArticle.aspx?ID=256 希望能有所帮助,     

要回复问题请先登录注册