Impersonation and PrincipalPermission

From: Novice (6tc1ATqlinkDOTqueensuDOTca)
Date: 11/12/04


Date: Fri, 12 Nov 2004 10:23:01 -0800

Hey all, I wrote some impersonation code that works fine (i.e. I'm able to
get the name of the user being impersonated and it works as expected) - but
I'm trying to use a PrincipalPermission object to make a demand and it
doesn't seem to be working as expected.

Basically I'm impersonating the Administrator user and then when I try to
demand that the current user is the Administrator it doesn't work.

Here is the code I've tried using:
PrincipalPermission principalPerm = new PrincipalPermission("Administrator",
"Administrators");
try
{
        //System.Security.Principal.WindowsIdentity.GetCurrent().Impersonate();
        MessageBox.Show ("You are:
"+System.Security.Principal.WindowsIdentity.GetCurrent().Name+" and you have
been authenticated:
"+System.Security.Principal.WindowsIdentity.GetCurrent().IsAuthenticated, "My
Application's Name",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
        principalPerm.Demand();
}
catch (Exception exception)
{
        MessageBox.Show ("You are not an administrator.", "My Application's Name",
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
}

--------------------------------------
I've also tried this:
PrincipalPermission principalPerm = new
PrincipalPermission("MyMachineName\\Administrator", null);
and this:
PrincipalPermission principalPerm = new
PrincipalPermission("MyMachineName\\Administrator", "Administrators");
and I've even tried this:
PrincipalPermission principalPerm = new PrincipalPermission(null,
"Administrators");

But none of it seems to work - I'm doing the above within the context of a
stand-alone C# application (i.e. not in an ASP.NET application).

I don't think I should have to include the commented out line - but I've
also tried using it and not using it with no observable affect.

Thanks,
Novice


Quantcast