Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

request a pricipal permission

/*
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# split multiple options 
Csharp :: c# check port in remote pc 
Csharp :: Create a button in unity to show ad 
Csharp :: cant see my classes in inspector 
Csharp :: c# Sum of all the factors of a number 
Csharp :: curl rest api keycloak 
Csharp :: toLocalIsoString() vs toIsoString() 
Csharp :: button event trigger wpf 
Csharp :: drawing default serializedproperty unity 
Csharp :: wpf binding ancestor codebehind 
Csharp :: remove substring from string c# 
Csharp :: JavaScriptSerializer() and convert to base64 
Csharp :: c# array to label 
Csharp :: wpf listbox binding change style of selected item 
Csharp :: how prevent user remove file linux 
Csharp :: c# 10 null checl 
Csharp :: select top 5 in linq c# 
Csharp :: check if multiple variables are null c# 
Csharp :: how to check to see if the keyboard buttons are pressed in unity 
Csharp :: c# check if character is lowercase 
Csharp :: c# catch two exceptions in one block 
Csharp :: list c# 
Csharp :: c# reverse a string for loop 
Csharp :: How can I get my stripe customer ID? 
Csharp :: inheritance 
Csharp :: Entity framwork update parent entity added new sub entity 
Csharp :: json serialize object capitalization config 
Csharp :: index list c# 
Csharp :: c# winform get access token facebook 
Csharp :: DisplayUnitType revit api 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =