[PrincipalPermissionAttribute(SecurityAction.Demand, Role = @"BUILTINAdministrators")]
private static void AdministratorsCode()
{
Console.WriteLine("Administrators Only");
}
System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;
using System.Security.Permissions;
namespace ConsoleProgramRunAsAdmon
{
class Program
{
[PrincipalPermissionAttribute(SecurityAction.Demand, Role = @"BUILTINAdministrators")]
private static void AdministratorsCode()
{
Console.WriteLine("Administrators Only");
}
static void Main(string[] args)
{
try
{
System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
AdministratorsCode();
}
catch(Exception ex)
{
Console.WriteLine("An exception occurred : " + ex.Message);
}
Console.ReadKey();
}
}
}