tags:

views:

55

answers:

1

Hello, I'm trying to understand role based security and I have the following method:

    [PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
    static void Test()
    {
        //administratos only can call this code
    }

What I wanna do is that only users that are members of the Windows Administrators group can call this code, however, if I do the following, it works:

        GenericIdentity genericIdentity = new GenericIdentity("test", "test");
        GenericPrincipal genericPrincipal = new GenericPrincipal(genericIdentity, new string[] { "Administrators" });
        AppDomain.CurrentDomain.SetThreadPrincipal(genericPrincipal);

        Test();

So, how can I make it work only if the user is in the Administrators windows group?

thanks!

A: 

Have you set the PrincipalPolicy? You need that to check roles against Windows groups.

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
Paul Kearney - pk
where should I add this line of code? inside my protected method Test?
Bill K
You'd add it somewhere in your application startup code. You would want to have the policy set before you try to demand the security.
Paul Kearney - pk