I want to use declarative security to guarantee that my app is only run by a local admin on the machine. For example,
private void Window_Loaded(object sender, RoutedEventArgs e)
{
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
try
{
LoadUsers();
}
catch (System.Security.SecurityException)
{
MessageBox.Show("You must be a local administrator to run this application.");
System.Environment.Exit(1);
}
}
// You must be an admin to run this method...
[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
private void LoadUsers()
{
// etc.
}
That is all well and good; however, it would be nice if I could debug without first launching the IDE with “Run as Administrator”.
Question: Is there a way to get around this in the security declaration attribute? Or is there a different security demand I can use? Thanks!
I guess there’s more control with Imperative security in this case. One can see if the debugger is attached or not before making the security demand.