Exporter (0) Imprimer
Développer tout
Ce sujet n'a pas encore été évalué - Évaluez ce sujet

KeyContainerPermissionAttribute, classe

Mise à jour : novembre 2007

Autorise l'application au code des actions de sécurité relatives à KeyContainerPermission, à l'aide de la sécurité déclarative. Cette classe ne peut pas être héritée.

Espace de noms :  System.Security.Permissions
Assembly :  mscorlib (dans mscorlib.dll)

[SerializableAttribute]
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple = true, 
	Inherited = false)]
[ComVisibleAttribute(true)]
public sealed class KeyContainerPermissionAttribute : CodeAccessSecurityAttribute
/** @attribute SerializableAttribute */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple = true, Inherited = false) */
/** @attribute ComVisibleAttribute(true) */
public final class KeyContainerPermissionAttribute extends CodeAccessSecurityAttribute
public final class KeyContainerPermissionAttribute extends CodeAccessSecurityAttribute

La portée autorisée de la déclaration dépend de la valeur SecurityAction utilisée.

Les informations de sécurité déclarées par un attribut de sécurité sont stockées dans les métadonnées de la cible de l'attribut et sont accessibles par le système au moment de l'exécution.KeyContainerPermissionAttribute n'est utilisé que pour la sécurité déclarative. Pour une sécurité impérative, utilisez la classe KeyContainerPermission.

L'exemple suivant montre l'utilisation des membres de la classe KeyContainerPermissionAttribute.

// The following commented code should be compiled as a console application.  The application 
// executes the class library code example showing the use of the KeyContainerPermissionAttribute.
//using System;
//using System.Security;
//using System.Security.Permissions;
//using System.Security.Cryptography;
//using KeyContainerPermissionAttributeDemo;
//namespace KeyContainerPermissionAttributeTest
//{
//  class AttributeTest
//  {
//       // Tests the KeyContainerPermissionAttribute.
//		[STAThread]
//		static void Main()
//		{
//            CspParameters cspParams = new CspParameters();
//            cspParams.KeyContainerName = "MyKeyContainer";
//            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);
//            
//            // Display the key information to the console.
//            Console.WriteLine("Key added to container: \n  {0}", rsa.ToXmlString(true));
//            KeyContainerUtil keyCU = new KeyContainerUtil();
//            // The following call creates a key container with the specified name if one doesn't exist.
//            // In this case the key container does exist.
//            keyCU.GenKey_SaveInContainer("MyKeyContainer");
//            // The following call deletes the key container.
//            keyCU.DeleteKeyFromContainer("MyKeyContainer");
//            // The next call to create a key container fails because the library
//            // routine does not have the permissions to create a key container.
//            keyCU.GenKey_SaveInContainer("MyKeyContainer");
//            Console.WriteLine("Press the Enter key to exit.");
//            Console.ReadKey();
//
//		}
//	}
//}

using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Cryptography;

// Prohibit the creation of a new key container.
[assembly :KeyContainerPermission(SecurityAction.RequestRefuse, Flags = KeyContainerPermissionFlags.Create,
                KeyContainerName = "MyKeyContainer", KeyStore = "User", 
                ProviderName = "Microsoft Strong Cryptographic Provider")]
namespace KeyContainerPermissionAttributeDemo
{
	public class KeyContainerUtil
	{
		public KeyContainerUtil()
		{	
		}
        // The following method throws an exception if the containerName parameter identifies a 
        // new container.  Otherwise the method executes correctly.
        public void GenKey_SaveInContainer(string containerName)
        {
            try
            {
                // Create the CspParameters object and set the name of the key  
                // container used to store the RSA key pair.
                CspParameters cspParams = new CspParameters();
                cspParams.KeyContainerName = containerName;
                // Create a new instance of RSACryptoServiceProvider that accesses
                // the key container specified by the containerName parameter.
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);

                // Display the key information to the console.
                Console.WriteLine("Key added to container: \n  {0}", rsa.ToXmlString(true));
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception thrown: " + e.Message);
            }
        }
        public void DeleteKeyFromContainer(string containerName)
        {
            try
            {
                // Create the CspParameters object and set the name of the key container 
                // used to store the RSA key pair, then delete the key entry in the container.
                CspParameters cspParams = new CspParameters();
                cspParams.KeyContainerName = containerName;
                // Create a new instance of RSACryptoServiceProvider that accesses
                // the key container MyKeycontainerName.
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspParams);
                // The following statement causes the key to be deleted by not persisting the key.
                rsa.PersistKeyInCsp = false;

                // Call Clear to release resources, deleting the key container.
                rsa.Clear();
                Console.WriteLine("Key and container deleted.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception thrown when deleting key: " + e.Message);
            }
        }
	}
}


package KeyContainerPermissionAttributeDemo ; 
// The following commented code should be compiled as a console application.  
// The application executes the class library code example showing the 
// use of the KeyContainerPermissionAttribute.
//    import System.*;
//    import System.Security.*;
//    import System.Security.Permissions.*;
//    import System.Security.Cryptography.*;
//    import KeyContainerPermissionAttributeDemo.*;
//    class AttributeTest
//    {
//        //Tests the KeyContainerPermissionAttribute.
//        /** @attribute STAThread()
//         */
//        static void main(String[] args)
//        {
//            CspParameters cspParams =  new CspParameters();
//            cspParams.keyContainerName = "MyKeyContainer";
//            RSACryptoServiceProvider rsa =  
//                new RSACryptoServiceProvider(cspParams);
//            // Display the key information to the console.
//            Console.WriteLine("Key added to container: \n  {0}", 
//                rsa.ToXmlString(true));
//            KeyContainerUtil keyCU =  new KeyContainerUtil();
//            // The following call creates a key container with the specified 
//            // name if one doesn't exist.
//            // In this case the key container does exist.
//            keyCU.GenKey_SaveInContainer("MyKeyContainer");
//            // The following call deletes the key container.
//            keyCU.DeleteKeyFromContainer("MyKeyContainer");
//            // The next call to create a key container fails because the 
//            // library routine does not have the permissions to create a key 
//            // container.
//            keyCU.GenKey_SaveInContainer("MyKeyContainer");
//            Console.WriteLine("Press the Enter key to exit.");
//            Console.ReadKey();
//        } //main 
//    } //AttributeTest

import System.*;
import System.Security.*;
import System.Security.Permissions.*;
import System.Security.Cryptography.*;

// Prohibit the creation of a new key container.
/** @assembly.class KeyContainerPermission(SecurityAction.RequestRefuse, 
    Flags = KeyContainerPermissionFlags.Create, 
    KeyContainerName = "MyKeyContainer", 
    KeyStore = "User", ProviderName = "Microsoft Strong Cryptographic Provider")
 */
public class KeyContainerUtil
{
    public KeyContainerUtil()
    {
    } //KeyContainerUtil

    // The following method throws an exception if the containerName 
    // parameter identifies a new container.  
    // Otherwise the method executes correctly.
    public void GenKey_SaveInContainer(String containerName)
    {
        try {
            // Create the CspParameters object and set the name of the key  
            // container used to store the RSA key pair.
            CspParameters cspParams = new CspParameters();
            cspParams.KeyContainerName = containerName;

            // Create a new instance of RSACryptoServiceProvider that accesses
            // the key container specified by the containerName parameter.
            RSACryptoServiceProvider rsa = 
                new RSACryptoServiceProvider(cspParams);

            // Display the key information to the console.
            Console.WriteLine("Key added to container: \n  {0}", 
                rsa.ToXmlString(true));
        }
        catch (System.Exception e) {
            Console.WriteLine(("Exception thrown: " + e.get_Message()));
        }
    } //GenKey_SaveInContainer

    public void DeleteKeyFromContainer(String containerName)
    {
        try {
            // Create the CspParameters object and set the name of the 
            // key container used to store the RSA key pair, then delete 
            // the key entry in the container.
            CspParameters cspParams = new CspParameters();
            cspParams.KeyContainerName = containerName;

            // Create a new instance of RSACryptoServiceProvider that accesses
            // the key container MyKeycontainerName.
            RSACryptoServiceProvider rsa = 
                new RSACryptoServiceProvider(cspParams);

            // The following statement causes the key to be deleted by 
            // not persisting the key.
            rsa.set_PersistKeyInCsp(false);

            // Call Clear to release resources, deleting the key container.
            rsa.Clear();
            Console.WriteLine("Key and container deleted.");
        }
        catch (System.Exception e) {
            Console.WriteLine(("Exception thrown when deleting key: " 
                + e.get_Message()));
        }
    } //DeleteKeyFromContainer
} //KeyContainerUtil   


Tous les membres static (Shared en Visual Basic) publics de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

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

Le .NET Framework et le .NET Compact Framework ne prennent pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

.NET Framework

Pris en charge dans : 3.5, 3.0, 2.0
Cela vous a-t-il été utile ?
(1500 caractères restants)
Merci pour vos suggestions.

Ajouts de la communauté

AJOUTER
Afficher:
© 2014 Microsoft. Tous droits réservés.