Help on PrincipalPermission Demand Throwing Exception

Discussion in 'MCTS' started by Abba Biya, Feb 7, 2007.

  1. Abba Biya

    Abba Biya Guest

    [PrincipalPermission(SecurityAction.Demand, Name=@"MYDomain\MyUserName")]
    static void TestMethod()
    {
    Console.WriteLine("Test Message");
    }

    public static void Main(string[] args)
    {
    WindowsIdentity wID = WindowsIdentity.GetCurrent();
    Console.WriteLine("Current User: {0}", wID .Name);

    try
    {
    TestMethod();
    }
    catch (System.Security.SecurityException ex)
    {
    Console.WriteLine("Exception: {0}", ex.Message);
    }

    Console.Read();
    }
    Abba Biya, Feb 7, 2007
    #1
    1. Advertising

  2. Abba Biya

    Abba Biya Guest

    Shoud add the following line for it to work.

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);


    some observations:

    Security Principals listed on separate demands will be OR whereas if listed
    all in one SecurityAction.Demand, it will be AND.

    [PrincipalPermission(SecurityActioin.Demand, Authenticated = true, Role =
    @"CONTOSO\Admins")]
    will allow only authenticated and users from Admins group.

    If we change it to the following:
    [PrincipalPermission(SecurityActioin.Demand, Authenticated = true)]
    [PrincipalPermission(SecurityAction.Demand, Role = @"CONTOSO\Admins")]

    Authenticated users with out being Admins will be able to run this code.
    this is a Logical OR.

    This is great.



    "Abba Biya" wrote:

    > [PrincipalPermission(SecurityAction.Demand, Name=@"MYDomain\MyUserName")]
    > static void TestMethod()
    > {
    > Console.WriteLine("Test Message");
    > }
    >
    > public static void Main(string[] args)
    > {
    > WindowsIdentity wID = WindowsIdentity.GetCurrent();
    > Console.WriteLine("Current User: {0}", wID .Name);
    >
    > try
    > {
    > TestMethod();
    > }
    > catch (System.Security.SecurityException ex)
    > {
    > Console.WriteLine("Exception: {0}", ex.Message);
    > }
    >
    > Console.Read();
    > }
    >
    Abba Biya, Feb 7, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page