Using SecurityAction Enumeration Flags

The SecurityAction enumeration is used in declarative security. Declarative security is when attributes are added to assemblies, class or methods.

The below SecurityAction’s relate to the permssions of the calling assemblies:

Demand
This demands that assembly stack have the permissions required
LinkDemand
This demands that only the initial calling assembly has the permissions

Below is an example of it in use:


using System;
using System.IO;
using System.Security.Permissions;

[assembly: FileIOPermission(SecurityAction.Demand, Unrestricted = true)]
namespace DemandTestApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Creating files...");
            File.Create("myFile.txt");
        }
    }
}

The below SecurityActions relate to permissions that your code requires to run

RequestMinimum
This permission check is run when it tries to load the application.
RequestOptional
This permission check is run when it tries to load the application.

Below is an example of RequestMinimum is use:


using System;
using System.IO;
using System.Security.Permissions;

[assembly: FileIOPermission(SecurityAction.RequestMinimum, Unrestricted = true)]
namespace RequestMinimumTestApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Creating files...");
            File.Create("myFile.txt");
        }
    }
}

If the RequestMinimum check fails when loading the assembly, you will get an error like this:


Could not load file or assembly 'MyTestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 
or one of its dependencies. Failed to grant minimum permission requests. 
(Exception from HRESULT: 0x80131417)":"MyTestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}.




Comments

No comments yet.

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Robert

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning hands down.