2012-08-23 49 views
7

Tôi có một phương thức của một lớp (DPCal_EventMove) mà tôi muốn giới hạn quyền truy cập bằng cách sử dụng Vai trò. Tôi có cả trình xử lý lỗi Global.asax.cs và trình xử lý lỗi IHttpModule tùy chỉnh nhằm nắm bắt các ngoại lệ không được xử lý và Server.Transfer chúng thành GlobalExceptionHandler.aspx, kiểm tra xem các lỗi là SecurityExceptions có nguồn gốc từ các kiểm tra PrincipalPermission không thành công hay không. Vì một lý do nào đó, ngoại lệ không được giải quyết gây ra bởi phương thức được trang trí bằng PricipalPermission không được định tuyến thông qua một trong các trình xử lý lỗi của tôi. Câu hỏi của tôi là: Trường hợp ngoại lệ này được định tuyến đến đâu và làm thế nào để tôi nắm bắt và xử lý nó?Ngoại lệ chưa xử lý không bị xử lý lỗi Global.asax hoặc trình xử lý lỗi IHttpModule tùy chỉnh

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(); 
     } 
    } 
} 

Dưới đây là tập tin Global.asax.cs của tôi:

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

Dưới đây là tùy chỉnh IHttpModule xử lý của tôi:

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 trên GlobalExceptionHandler.aspx không bao giờ đạt được.

+0

Tôi nghĩ rằng liên kết này có thể có một số thông tin tốt cho bạn: http://stackoverflow.com/questions/2192093/wcf-principalpermission-attribute-exception-loggin –

+1

Nếu bạn gặp lỗi trong khi gọi lại trang qua một WebMethod , bạn sẽ phải xử lý lỗi một cách thích hợp ở phía máy khách. Có thể bạn có thể gửi một phiên bản (đơn giản) của mã của bạn khi (1) gọi 'DPCal_EventMove' và (2) định nghĩa' DPCal_EventMove'? –

Trả lời

1

Hóa ra là vấn đề đã được gây ra bởi vì phương pháp DPCal_EventMove được thực hiện như một callback trang. May mắn thay, thành phần lịch DayPilot có một tùy chọn để thay đổi hành vi này. Khi tôi đã thay đổi thuộc tính EventMoveHandling của DayPilot: Kiểm soát DayPilotCalendar thành "PostBack" thay vì "CallBack", tôi đã có thể nắm bắt và xử lý ngoại lệ bảo mật.

+0

Thành phần mà tôi đang đề cập đến là: http://www.daypilot.org/ – Matt

0

Các bạn đã thử:

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

Tôi không chắc chắn làm thế nào mà có thể giúp đỡ. Application_Error không bao giờ được gọi (được xác nhận bằng cách đặt một điểm ngắt ở đầu phương thức). – Matt

+0

Tôi đã xác minh mã trên trên một dự án đơn giản (chỉ để cho sự hài lòng của riêng tôi), và thực sự, ClearErrors() là không cần thiết nếu bạn thực hiện chuyển khoản. Có thể bạn đang ghi đè hành vi tiêu chuẩn bằng nút CustomError trong web.config của bạn không? –

+0

là gì trong web.config của tôi – Matt

0

Add:

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

Để mã của trang của bạn và xem nếu điều đó được gọi là trong suốt một lỗi?