Link to home
Start Free TrialLog in
Avatar of seaero
seaero

asked on

How do I catch and handle a Security Exception thrown when using DECLARATIVE security on a method?

I have a method with Declarative security defined as such:

[PrincipalPermissionAttribute(SecurityAction.Demand, Name="test", Role="Administrator")]
private void MyButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
   // do something
}

Open in new window


Whenever I click on the button which executes the MyButton_Click() method, a SecurityException is thrown (as expected) because the user does not have the proper permissions to access the method.  My problem is that I cannot seem to catch the SecurityException that is being thrown in order to handle it gracefully.

I've tried adding try/catch blocks within the method as well as in the constructor of the WPF Window, and in the application Main() method. It seems that no matter where I put the try/catch, I cannot catch the SecurityException. Any clues?
ASKER CERTIFIED SOLUTION
Avatar of Sudhakar Pulivarthi
Sudhakar Pulivarthi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of seaero
seaero

ASKER

Since I am not using WCF, I couldn't implement the IErrorhandler in my WPF application.

However, the links you provided helped me find what I was looking for. I basically ended up using a Global Exception Handler at the app level with DispatcherUnhandledExceptionEventHandler as outlined here: http://www.devx.com/tips/Tip/42431

I will give you the points for attempting to answer. Thanks!
Avatar of seaero

ASKER

Didn't actually solve my problem, but provided links that eventually helped me find the answer.