Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

administration

/*
A more standard approach (but obsolete) to demand in code that the current user has Admin privileges
before running one or more guarded code is to use the PrincipalPermissionAttribute
for the guarded method of your program.

The following is an example :
*/

// NOTE PrincipalPermissionAttribute.PrincipalPermissionAttribute(SecurityAction) is obsolete
[PrincipalPermissionAttribute(SecurityAction.Demand, Role = @"BUILTINAdministrators")]
private static void AdministratorsCode()
{
  Console.WriteLine("Administrators Only");
}

/*
Declare a usage of the System.Security.Principal and System.Security.Permissions namespaces.
Early in your program, before calling a method like AdministratorsCode(), you need to call :
*/

System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

/*
Below is a complete example code :
*/

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();
        }
    }
}

// Hope that helps
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# progress bar timer 
Csharp :: Advertisement code for unity 
Csharp :: unity cannot click button 
Csharp :: install nuget package for S3 
Csharp :: c# .net core entity framework one to many 
Csharp :: c# lambda group by multiple columns 
Csharp :: c# short to int 
Csharp :: unity array c# 
Csharp :: c# asp.net hover tooltip 
Csharp :: reference a class by string unity 
Csharp :: c# convert string to array 
Csharp :: unity awake 
Csharp :: how to use open hardware monitor in c# 
Csharp :: c# copy bidimensional array 
Csharp :: the name scripts does not exist in the current context mvc 5 
Csharp :: check if element in hashset c# 
Csharp :: how to compare time strings in c# 
Csharp :: dataset empty check C# 
Csharp :: c sharp async 
Csharp :: c# mysql select into datatable 
Csharp :: Using Linq to get the last N elements of a collection? C# 
Csharp :: c# return multiple values 
Csharp :: c# extension 
Csharp :: if else c# 
Csharp :: FiveM pc key code 
Csharp :: c# quick "is" "as" 
Csharp :: Moq Unittest with ILogger 
Csharp :: aspnet for loop 
Csharp :: how to make font factory text to bold in c# 
Csharp :: windows forms change double buffer during runtime 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =