Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

pricipal permission attribute in c#

/*
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 :: Send Hotmail, Outlook, Office365 Email using SMTP C# .NET 
Csharp :: c# split quotation 
Csharp :: print hello world in unity 
Csharp :: web client ignore ssl error 
Csharp :: referans tipi nedir c# 
Csharp :: sto playing audiosource 
Csharp :: Ignore case string linq c# 
Csharp :: Unlit shader get the direction of camera UNity 
Csharp :: discord embeds how to separate inline fields 
Csharp :: declare multiple variables in for loop C# 
Csharp :: stroke dash array wpf 
Csharp :: c# guid from string 
Csharp :: dapper get list 
Csharp :: Non-Serialized Fields in unity inspector 
Csharp :: static constructor in c# 
Csharp :: How to get selected item from Dropdown in GridView 
Csharp :: console writeline 
Csharp :: commit help 
Csharp :: c# anonymous type as return value 
Csharp :: Get replace normal text from word document in C# 
Csharp :: instantiate c# 
Csharp :: c# switch example 
Csharp :: datetime show 24 hour format c# 
Csharp :: truncate c# 
Csharp :: add to ienumerable 
Csharp :: logical operators in c# 
Csharp :: How do I call a string inside a different class 
Csharp :: wpf fixed window size 
Csharp :: Bedingungen in C# – if, else und else if 
Csharp :: transform.lookat 2d 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =