Re: PrincipalPermissionAttribute basic help

From: Willy Denoyette [MVP] (willy.denoyette@pandora.be)
Date: 07/20/02


From: "Willy Denoyette [MVP]" <willy.denoyette@pandora.be>
Date: Sat, 20 Jul 2002 12:14:56 +0200


You have to override the default principal policy (UnauthenticatedPrincipal) by calling:

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

    WindowsIdentity wi = WindowsIdentity.GetCurrent();
    .....

Willy.

"Ed Maillet" <emaillet@_nospam_.unum.com> wrote in message news:1839a01c22f70$5edc6560$2ae2c90a@hosting.microsoft.com...
> Hey all,
> I can't seem to make the most basic
> PrincipalPermissionAttribute usage work right.
> The simple code below when run shows that IsAuthenticated
> is true but the demand fails. I feel like I'm missing
> something totally basic here. Any ideas?
>
> ----- Ed
>
>
>
> The code:
>
> using System;
> using System.Security.Permissions;
> using System.Security.Principal;
>
> class Class2
> {
> [PrincipalPermissionAttribute(SecurityAction.Demand,
> Authenticated=true)]
> public void PrivateInfo()
> {
> //Print secret data.
> Console.WriteLine("\n\nYou have access to the
> private data!");
> }
>
> }
>
> class Class1
> {
> static void Main()
> {
> Class2 c2 = new Class2();
>
> WindowsIdentity wi = WindowsIdentity.GetCurrent();
> WindowsPrincipal wp = new WindowsPrincipal(wi);
> Console.WriteLine("Authed:
> {0}",wp.Identity.IsAuthenticated);
> try
> {
> c2.PrivateInfo();
> }
> catch
> {
> Console.WriteLine("PrivateInfo failed");
> }
> }
> }
>


Loading