Questa documentazione è stata archiviata e non viene gestita.

Classe PermissionSet

Aggiornamento: novembre 2007

Rappresenta un insieme contenente numerosi tipi diversi di autorizzazioni.

Spazio dei nomi:  System.Security
Assembly:  mscorlib (in mscorlib.dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
[StrongNameIdentityPermissionAttribute(SecurityAction.InheritanceDemand, Name = "mscorlib", 
	PublicKey = "0x00000000000000000400000000000000")]
public class PermissionSet : ISecurityEncodable, 
	ICollection, IEnumerable, IStackWalk, IDeserializationCallback
/** @attribute SerializableAttribute */ 
/** @attribute ComVisibleAttribute(true) */
/** @attribute StrongNameIdentityPermissionAttribute(SecurityAction.InheritanceDemand, Name = "mscorlib", PublicKey = "0x00000000000000000400000000000000") */
public class PermissionSet implements ISecurityEncodable, 
	ICollection, IEnumerable, IStackWalk, IDeserializationCallback
public class PermissionSet implements ISecurityEncodable, ICollection, IEnumerable, IStackWalk, IDeserializationCallback

È possibile utilizzare PermissionSet per eseguire operazioni su più autorizzazioni diverse come se costituissero un gruppo.

Nell'esempio di codice riportato di seguito viene illustrato l'utilizzo della classe PermissionSet e dei membri.

// This sample demonstrates the use of the PermissionSet class.

using System;
using System.Reflection;
using System.Security.Permissions;
using System.Security;
using System.IO;
using System.Collections;

class MyClass
{
    public static void PermissionSetDemo()
    {
        Console.WriteLine("Executing PermissionSetDemo");
        try
        {
            // Open a new PermissionSet.
            PermissionSet ps1 = new PermissionSet(PermissionState.None);
            Console.WriteLine("Adding permission to open a file from a file dialog box.");
            // Add a permission to the permission set.
            ps1.AddPermission(
                new FileDialogPermission(FileDialogPermissionAccess.Open));
            Console.WriteLine("Demanding permission to open a file.");
            ps1.Demand();
            Console.WriteLine("Demand succeeded.");
            Console.WriteLine("Adding permission to save a file from a file dialog box.");
            ps1.AddPermission(
                new FileDialogPermission(FileDialogPermissionAccess.Save));
            Console.WriteLine("Demanding permission to open and save a file.");
            ps1.Demand();
            Console.WriteLine("Demand succeeded.");
            Console.WriteLine("Adding permission to read environment variable USERNAME.");
            ps1.AddPermission(
                new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME"));
            ps1.Demand();
            Console.WriteLine("Demand succeeded.");
            Console.WriteLine("Adding permission to read environment variable COMPUTERNAME.");
            ps1.AddPermission(
                new EnvironmentPermission(EnvironmentPermissionAccess.Write,
                "COMPUTERNAME"));
            // Demand all the permissions in the set.
            Console.WriteLine("Demand all permissions.");
            ps1.Demand();
            Console.WriteLine("Demand succeeded.");
            // Display the number of permissions in the set.
            Console.WriteLine("Number of permissions = " + ps1.Count);
            // Display the value of the IsSynchronized property.
            Console.WriteLine("IsSynchronized property = " + ps1.IsSynchronized);
            // Display the value of the IsReadOnly property.
            Console.WriteLine("IsReadOnly property = " + ps1.IsReadOnly);
            // Display the value of the SyncRoot property.
            Console.WriteLine("SyncRoot property = " + ps1.SyncRoot);
            // Display the result of a call to the ContainsNonCodeAccessPermissions method.
            // Gets a value indicating whether the PermissionSet contains permissions
            // that are not derived from CodeAccessPermission.
            // Returns true if the PermissionSet contains permissions that are not
            // derived from CodeAccessPermission; otherwise, false.
            Console.WriteLine("ContainsNonCodeAccessPermissions method returned " +
                ps1.ContainsNonCodeAccessPermissions());
            Console.WriteLine("Value of the permission set ToString = \n" + ps1.ToString());
            PermissionSet ps2 = new PermissionSet(PermissionState.None);
            // Create a second permission set and compare it to the first permission set.
            ps2.AddPermission(
                new EnvironmentPermission(EnvironmentPermissionAccess.Read, "USERNAME"));
            ps2.AddPermission(
                new EnvironmentPermission(EnvironmentPermissionAccess.Write, "COMPUTERNAME"));
            IEnumerator list =  ps1.GetEnumerator();
            Console.WriteLine("Permissions in first permission set:");
            while (list.MoveNext())
                Console.WriteLine(list.Current.ToString());
            Console.WriteLine("Second permission IsSubsetOf first permission = " + ps2.IsSubsetOf(ps1));
            // Display the intersection of two permission sets.
            PermissionSet ps3 = ps2.Intersect(ps1);
            Console.WriteLine("The intersection of the first permission set and "
                + "the second permission set = " + ps3.ToString());
            // Create a new permission set.
            PermissionSet ps4 = new PermissionSet(PermissionState.None);
            ps4.AddPermission(
                new FileIOPermission(FileIOPermissionAccess.Read,
                "C:\\Temp\\Testfile.txt"));
            ps4.AddPermission(
                new FileIOPermission(FileIOPermissionAccess.Read |
                FileIOPermissionAccess.Write | FileIOPermissionAccess.Append,
                "C:\\Temp\\Testfile.txt"));
            // Display the union of two permission sets.
            PermissionSet ps5 = ps3.Union(ps4);
            Console.WriteLine("The union of permission set 3 and permission set 4 = "
                + ps5.ToString());
            // Remove FileIOPermission from the permission set.
            ps5.RemovePermission(typeof(FileIOPermission));
            Console.WriteLine("The last permission set after removing FileIOPermission = "
                + ps5.ToString());
            // Change the permission set using SetPermission.
            ps5.SetPermission(new EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, "USERNAME"));
            Console.WriteLine("Permission set after SetPermission = " + ps5.ToString());
            // Display result of ToXml and FromXml operations.
            PermissionSet ps6 = new PermissionSet(PermissionState.None);
            ps6.FromXml(ps5.ToXml());
            Console.WriteLine("Result of ToFromXml = " + ps6.ToString() + "\n");
            // Display results of PermissionSet.GetEnumerator.
            IEnumerator psEnumerator = ps1.GetEnumerator();
            while (psEnumerator.MoveNext())
            {
                Console.WriteLine(psEnumerator.Current);
            }
            // Check for an unrestricted permission set.
            PermissionSet ps7 = new PermissionSet(PermissionState.Unrestricted);
            Console.WriteLine("Permission set is unrestricted = " + ps7.IsUnrestricted());
            // Create and display a copy of a permission set.
            ps7 = ps5.Copy();
            Console.WriteLine("Result of copy = " + ps7.ToString());
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }

    }


    static void Main(string[] args)
    {
        PermissionSetDemo();
    }

}


// This sample demonstrates the use of the PermissionSet class.

import System.*;
import System.Reflection.*;
import System.Security.Permissions.*;
import System.Security.*;
import System.IO.*;
import System.Collections.*;

class MyClass
{
	public static void PermissionSetDemo()
	{
		Console.WriteLine("Executing PermissionSetDemo");
		try
		{
			// Open a new PermissionSet.
			PermissionSet ps1 = new PermissionSet(PermissionState.None);

			Console.WriteLine("Adding permission to open a file"
				+ " from a file dialog box.");

			// Add a permission to the permission set.
			ps1.AddPermission(new FileDialogPermission(
				FileDialogPermissionAccess.Open));

			Console.WriteLine("Demanding permission to open a file.");
			ps1.Demand();
			Console.WriteLine("Demand succeeded.");
			Console.WriteLine("Adding permission to save"
				+ " a file from a file dialog box.");
			ps1.AddPermission(new FileDialogPermission(
				FileDialogPermissionAccess.Save));
			Console.WriteLine("Demanding permission to open and save a file.");
			ps1.Demand();
			Console.WriteLine("Demand succeeded.");
			Console.WriteLine("Adding permission to read "
				+ "environment variable USERNAME.");
			ps1.AddPermission(new EnvironmentPermission(
				EnvironmentPermissionAccess.Read, "USERNAME"));
			ps1.Demand();
			Console.WriteLine("Demand succeeded.");
			Console.WriteLine("Adding permission to read "
				+ "environment variable COMPUTERNAME.");
			ps1.AddPermission(new EnvironmentPermission(
				EnvironmentPermissionAccess.Write, "COMPUTERNAME"));

			// Demand all the permissions in the set.
			Console.WriteLine("Demand all permissions.");
			ps1.Demand();

			Console.WriteLine("Demand succeeded.");

			// Display the number of permissions in the set.
			Console.WriteLine(("Number of permissions = " + ps1.get_Count()));

			// Display the value of the IsSynchronized property.
			Console.WriteLine(("IsSynchronized property = "
				+ System.Convert.ToString(ps1.get_IsSynchronized())));

			// Display the value of the IsReadOnly property.
			Console.WriteLine(("IsReadOnly property = "
				+ System.Convert.ToString(ps1.get_IsReadOnly())));

			// Display the value of the SyncRoot property.
			Console.WriteLine(("SyncRoot property = " + ps1.get_SyncRoot()));

			// Display the result of a call to the 
			// ContainsNonCodeAccessPermissions method.
			// Gets a value indicating whether the PermissionSet 
			// contains permissions
			// that are not derived from CodeAccessPermission.
			// Returns true if the PermissionSet contains permissions that are 
			// not derived from CodeAccessPermission; otherwise, false.
			Console.WriteLine(
				("ContainsNonCodeAccessPermissions method returned "
					+ System.Convert.ToString(
					ps1.ContainsNonCodeAccessPermissions())));

			Console.WriteLine(("Value of the permission set ToString = \n"
				+ ps1.ToString()));

			PermissionSet ps2 = new PermissionSet(PermissionState.None);

			// Create a second permission set and compare it to the
			// first permission set.
			ps2.AddPermission(new EnvironmentPermission(
				EnvironmentPermissionAccess.Read, "USERNAME"));
			ps2.AddPermission(new EnvironmentPermission(
				EnvironmentPermissionAccess.Write, "COMPUTERNAME"));
			IEnumerator list = ps1.GetEnumerator();
			Console.WriteLine("Permissions in first permission set:");
			while (list.MoveNext())
				Console.WriteLine(list.get_Current().ToString());
			Console.WriteLine(
				("Second permission IsSubsetOf first permission = "
				+ System.Convert.ToString(ps2.IsSubsetOf(ps1))));

			// Display the intersection of two permission sets.
			PermissionSet ps3 = ps2.Intersect(ps1);
			Console.WriteLine(
				("The intersection of the first permission set and "
				+ "the second permission set = " + ps3.ToString()));

			// Create a new permission set.
			PermissionSet ps4 = new PermissionSet(PermissionState.None);
			ps4.AddPermission(new FileIOPermission(
				FileIOPermissionAccess.Read, "C:\\Temp\\Testfile.txt"));
			ps4.AddPermission(new FileIOPermission(
				FileIOPermissionAccess.Read | FileIOPermissionAccess.Write
				| FileIOPermissionAccess.Append, "C:\\Temp\\Testfile.txt"));

			// Display the union of two permission sets.
			PermissionSet ps5 = ps3.Union(ps4);
			Console.WriteLine(
				("The union of permission set 3 and permission set 4 = "
				+ ps5.ToString()));

			// Remove FileIOPermission from the permission set.
			ps5.RemovePermission(FileIOPermission.class.ToType());
			Console.WriteLine(
				("The last permission set after removing FileIOPermission = "
				+ ps5.ToString()));

			// Change the permission set using SetPermission.
			ps5.SetPermission(new EnvironmentPermission(
				EnvironmentPermissionAccess.AllAccess, "USERNAME"));
			Console.WriteLine(
				("Permission set after SetPermission = "
				+ ps5.ToString()));

			// Display result of ToXml and FromXml operations.
			PermissionSet ps6 = new PermissionSet(PermissionState.None);
			ps6.FromXml(ps5.ToXml());
			Console.WriteLine(("Result of ToFromXml = "
				+ ps6.ToString() + "\n"));

			// Display results of PermissionSet.GetEnumerator.
			IEnumerator psEnumerator = ps1.GetEnumerator();
			while (psEnumerator.MoveNext())
			{
				Console.WriteLine(psEnumerator.get_Current());
			}

			// Check for an unrestricted permission set.
			PermissionSet ps7 = new PermissionSet(PermissionState.Unrestricted);
			Console.WriteLine(
				("Permission set is unrestricted = "
				+ System.Convert.ToString(ps7.IsUnrestricted())));

			// Create and display a copy of a permission set.
			ps7 = ps5.Copy();
			Console.WriteLine(("Result of copy = " + ps7.ToString()));
		}
		catch (System.Exception e)
		{
			Console.WriteLine(e.get_Message().ToString());
		}
	} //PermissionSetDemo

	public static void main(String[] args)
	{
		PermissionSetDemo();
	} //main
} //MyClass


Qualsiasi membro static (Shared in Visual Basic) pubblico di questo tipo è thread-safe. I membri di istanza non sono garantiti come thread-safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.

.NET Framework

Supportato in: 3.5, 3.0, 2.0, 1.1, 1.0
Mostra: