When I employ the PrincipalPermission attribute for a method in a class, and the user is not in the role specified, the application throws an exception. Example code from SalesModuleController.cs file:
private void AddPageLinks(NavViewContext context)
{
context.AddNavTaskLink("Customers", ShowCustomersPageHandler, ResourceNames.CustomersImage);
context.AddNavTaskLink("Sales Orders", ShowSalesOrdersPagePageHandler, ResourceNames.UserImage);
AddPageLinksForMarketing(context);
// Add NavBar Links Here <<*** Wizard Marker - Do not edit, move or delete ***>>
}
[PrincipalPermission(SecurityAction.Demand, Role="Marketing")]
private void AddPageLinksForMarketing(NavViewContext context)
{
context.AddNavTaskLink("Leads", ShowLeadMasterPagePageHandler, ResourceNames.UserImage);
context.AddNavTaskLink("Markets", ShowMarketMasterPagePageHandler, ResourceNames.SalesOrdersImage);
}
Now, what is happening should happen...a SecurityException is thrown because a user does not belong to the Marketing group. The problem is that I am having trouble handling that exception in CAB/DF. The code bombs at this point in the ShellApplication.cs file (marked in red):
private static void HandleException(Exception ex)
{
if (ex == null)
return;
// ToDo: Intercept LoginCancelException and terminate the app
ExceptionPolicy.HandleException(ex, "default Policy");
// ToDo: Do a better job of unfolding the exception and dump it into the debug log as well.
MessageBox.Show("An unhandled exception occurred, and the application is terminating. For more information, see your Application event log.");
Application.Exit();
}
The initial exception listed is a ModuleLoadException, not a SecurityException, as expected. The SecurityException is baked into the InnerException information.
So, how do I properly capture this SecurityException? I have tried several variations in the app.config file, but I cannot seem to get to the right combination. I hope you can provide some direction on this.
Bill
Edited by Linguinut - 06-Dec-2007 at 4:39pm