笔记11

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Security.Principal;

namespace WindowsPrincipalTest
{
    
class Program
    {
        
static void Main(string[] args)
        {
            DemonstrateWindowsBuiltInRoleEnum();
            
            Console.ReadKey();
        }

        
public static void DemonstrateWindowsBuiltInRoleEnum()
        {
            AppDomain myDomain 
= Thread.GetDomain();

            myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
            WindowsPrincipal myPrincipal 
= (WindowsPrincipal)Thread.CurrentPrincipal;


            Console.WriteLine(
"{0} belongs to: ", myPrincipal.Identity.Name.ToString());

            Array wbirFields 
= Enum.GetValues(typeof(WindowsBuiltInRole));

            
foreach (object roleName in wbirFields)
            {
                
try
                {
                    Console.WriteLine(
"{0}? {1}.", roleName,
                        myPrincipal.IsInRole((WindowsBuiltInRole)roleName));
                }
                
catch (Exception)
                {
                    Console.WriteLine(
"{0}: Could not obtain role for this RID.",
                        roleName);
                }
            }

        }

    }

}
posted @ 2008-04-19 23:45  李涛  阅读(143)  评论(0)    收藏  举报