Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

demand a Security action 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 :: sustituir un caracter de un string c# 
Csharp :: Convert integers to written numbers C# 
Csharp :: if input.get touch 
Csharp :: access label from another class c# 
Csharp :: dictionaries in unity 
Csharp :: Lambda Expression to filter a list of list of items 
Csharp :: .net mvc foreach index 
Csharp :: blazor image button 
Csharp :: string is int f# 
Csharp :: c# convert excel column index to letter 
Csharp :: unity c# image invisible 
Csharp :: C# checking if a value is a int 
Csharp :: How to change ListBox selection background color 
Csharp :: do while loop in c# 
Csharp :: boxing and unboxing in c# 
Csharp :: ##[error]Dotnet command failed with non-zero exit code on the following projects 
Csharp :: convert date to days c# 
Csharp :: page parent wpf 
Csharp :: Generic Stack in c# 
Csharp :: c# string verbatim 
Csharp :: hashtable in c# 
Csharp :: c# return values 
Csharp :: c# code examples 
Csharp :: 2d explosion unity 
Csharp :: c# out argument 
Csharp :: how to get the dynamic year for your web app in mvc 
Csharp :: unity SceneTemplatePipeline 
Csharp :: Count Rows of table using Linq 
Csharp :: find the values of dictionaries in C sharp 
Csharp :: html inside razor 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =