2012-08-23 260 views
7

我有一个类(DPCal_EventMove)的方法,我想限制使用角色的访问权限。我有一个Global.asax.cs错误处理程序和一个自定义IHttpModule错误处理程序,用于捕获未处理的异常和Server。将它们转换为GlobalExceptionHandler.aspx,该错误处理程序检查错误是否源自失败的PrincipalPermission检查的SecurityException。出于某种原因,由PricipalPermission-decorated方法引起的未处理的异常不会通过任何一个错误处理程序进行路由。我的问题是:这个异常被传送到哪里以及如何捕获和处理它?未处理的异常未被Global.asax错误处理程序或自定义IHttpModule错误处理程序捕获

public partial class DayView : Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     // Do some stuff 
    } 

    [PrincipalPermission(SecurityAction.Demand, Role = "Investigator")] 
    [PrincipalPermission(SecurityAction.Demand, Role = "Administrator")] 
    protected void DPCal_EventMove(object sender, DayPilot.Web.Ui.Events.EventMoveEventArgs e) 
    { 
     // If no overlap, then save 
     int eventId = Convert.ToInt32(e.Value); 
     MembershipUser user = Membership.GetUser(); 
     if (!CommonFunctions.IsSchedulingConflict(eventId, e.NewStart, e.NewEnd) && 
      Page.User.HasEditPermission(user, eventId)) 
     { 
      dbUpdateEvent(eventId, e.NewStart, e.NewEnd); 
      GetEvents(); 
      DPCal.Update(); 
     } 
    } 
} 

下面是我的Global.asax.cs文件:

public class Global : System.Web.HttpApplication 
{ 
    protected void Application_Error(object sender, EventArgs e) 
    { 
     Server.Transfer("~/GlobalExceptionHandler.aspx?ReturnUrl=" + Request.Path); 
    } 
} 

下面是我的自定义IHttpModule的处理程序:永远不会达成GlobalExceptionHandler.aspx

public class UnhandledExceptionModule : IHttpModule 
{ 
    private HttpApplication _context; 
    private bool _initialized = false; 

    public void Init(HttpApplication context) 
    { 
     _context = context; 
     _initialized = true; 
     context.Error += new EventHandler(Application_Error); 
    } 

    public UnhandledExceptionModule() 
    { 
     _initialized = false; 
    } 

    public void Dispose() 
    { 
     if (_initialized) 
      _context.Dispose(); 
    } 

    public void Application_Error(object sender, EventArgs e) 
    { 
     if (_initialized) 
      _context.Server.Transfer("~/GlobalExceptionHandler.aspx?ReturnUrl=" + _context.Request.Path); 
    } 
} 

的Page_Load。

+0

我认为这个链接可能有一些很好的信息给你:http://stackoverflow.com/questions/2192093/wcf-principalpermission-attribute-exception-loggin –

+1

如果你在通过WebMethod进行页面回调时出错,你将不得不在客户端处理错误。 (1)调用'DPCal_EventMove'和(2)'DPCal_EventMove'的定义时,您可能会发布一个(简化版)代码吗? –

回答

1

原来,这个问题是因为DPCal_EventMove方法作为页面回调执行造成的。幸运的是,DayPilot日历组件可以选择更改此行为。一旦我将DayPilot的EventMoveHandling属性DayPilotCalendar控件更改为“PostBack”而不是“CallBack”,我就能够捕获并处理安全异常。

+0

我指的是这个组件:http://www.daypilot.org/ – Matt

0

你试过:

public class Global : System.Web.HttpApplication 
{ 
    protected void Application_Error(object sender, EventArgs e) 
    { 
     Server.Transfer("~/GlobalExceptionHandler.aspx?ReturnUrl=" + Request.Path); 
    } 
} 
+0

我不知道如何可以帮助。 Application_Error永远不会被调用(通过在方法的开始处放置一个断点来确认)。 – Matt

+0

我已经在一个简单的项目上验证了上面的代码(仅仅为了我自己的满足感),并且实际上,如果您执行传输,则不需要ClearErrors()。是否有可能使用web.config中的CustomError节点覆盖标准行为? –

+0

是什么在我的web.config – Matt

0

地址:

private void Page_Error(object sender, System.EventArgs e) 
{ 
     //errorcode 
} 

要在网页的代码,看看这是一个错误时叫什么名字?