KeyContainerPermission クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > KeyContainerPermission クラスの意味・解説 

KeyContainerPermission クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

キー コンテナアクセスする権利制御します。このクラス継承できません。

名前空間: System.Security.Permissions
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 KeyContainerPermission
    Inherits CodeAccessPermission
    Implements IUnrestrictedPermission
Dim instance As KeyContainerPermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class KeyContainerPermission
 : CodeAccessPermission, IUnrestrictedPermission
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class KeyContainerPermission sealed
 : public CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class KeyContainerPermission extends
 CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class KeyContainerPermission extends
 CodeAccessPermission implements IUnrestrictedPermission
解説解説
使用例使用例

KeyContainerPermission クラスメンバ使用する方法次のコード例示します

Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Cryptography



Public Class KeyContainerPermissionDemo
    Private Shared cspParams As
 New CspParameters()
    Private Shared rsa As
 New RSACryptoServiceProvider()
    Private Shared providerName As
 String
    Private Shared providerType As
 Integer
    Private Shared myKeyContainerName As
 String
    ' Create three KeyContainerPermissionAccessEntry objects, each with
 a different constructor.
    Private Shared keyContainerPermAccEntry1
 As _
        New KeyContainerPermissionAccessEntry("MyKeyContainer",
 KeyContainerPermissionFlags.Create)
    Private Shared keyContainerPermAccEntry2
 As _
        New KeyContainerPermissionAccessEntry(cspParams, KeyContainerPermissionFlags.Open)
    Private Shared keyContainerPermAccEntry3
 As _
        New KeyContainerPermissionAccessEntry("Machine",
 providerName, providerType, _
            myKeyContainerName, 1, KeyContainerPermissionFlags.Open)


    Public Shared Function
 Main() As Integer
        Try
            ' Create a key container for use in the sample.
            GenKey_SaveInContainer("MyKeyContainer")
            ' Initialize property values for creating a KeyContainerPermissionAccessEntry
 object.
            myKeyContainerName = rsa.CspKeyContainerInfo.KeyContainerName
            providerName = rsa.CspKeyContainerInfo.ProviderName
            providerType = rsa.CspKeyContainerInfo.ProviderType
            cspParams.KeyContainerName = myKeyContainerName
            cspParams.ProviderName = providerName
            cspParams.ProviderType = providerType

            ' Display the KeyContainerPermissionAccessEntry properties
 using 
            ' the third KeyContainerPermissionAccessEntry object.
            DisplayAccessEntryMembers()
            ' Add access entry objects to a key container permission.
            Dim keyContainerPerm1 As New
 KeyContainerPermission(PermissionState.Unrestricted)
            Console.WriteLine("Is the permission unrestricted?
 " + _
                keyContainerPerm1.IsUnrestricted().ToString())
            keyContainerPerm1.AccessEntries.Add(keyContainerPermAccEntry1)
            keyContainerPerm1.AccessEntries.Add(keyContainerPermAccEntry2)
            ' Display the permission.
            System.Console.WriteLine(keyContainerPerm1.ToXml().ToString())

            ' Create an array of KeyContainerPermissionAccessEntry objects
            Dim keyContainerPermAccEntryArray As
 KeyContainerPermissionAccessEntry() = _
                {keyContainerPermAccEntry1, keyContainerPermAccEntry2}

            ' Create a new KeyContainerPermission using the array.
            Dim keyContainerPerm2 As _
                New KeyContainerPermission(KeyContainerPermissionFlags.AllFlags,
 keyContainerPermAccEntryArray)
            DisplayPermissionMembers(keyContainerPerm2, keyContainerPermAccEntryArray)

            ' Demonstrate the effect of a deny for opening a key container.
            DenyOpen()
            ' Demonstrate the deletion of a key container.         
  
            DeleteContainer()

            Console.WriteLine("Press the Enter key to exit.")
            Console.Read()
            Return 0
            ' Close the current try block that did not expect an exception.
        Catch e As Exception
            Console.WriteLine("Unexpected exception thrown:  "
 + e.Message)
            Return 0
        End Try

    End Function 'Main


    Private Shared Sub DisplayAccessEntryMembers()
        Console.WriteLine(vbLf + "KeycontainerName is "
 + _
            keyContainerPermAccEntry3.KeyContainerName)
        Console.WriteLine("KeySpec is " + IIf(1 =
 keyContainerPermAccEntry3.KeySpec, _
            "AT_KEYEXCHANGE ", "AT_SIGNATURE"))
        Console.WriteLine("KeyStore is " + keyContainerPermAccEntry3.KeyStore)
        Console.WriteLine("ProviderName is " + keyContainerPermAccEntry3.ProviderName)
        Console.WriteLine("ProviderType is " + IIf(1
 = keyContainerPermAccEntry3.ProviderType, _
            "PROV_RSA_FULL", keyContainerPermAccEntry3.ProviderType.ToString()))
        Console.WriteLine("Hashcode = " + keyContainerPermAccEntry3.GetHashCode().ToString())
        Console.WriteLine("Are the KeyContainerPermissionAccessEntry
 objects equal? " + _
            keyContainerPermAccEntry3.Equals(keyContainerPermAccEntry2).ToString())

    End Sub 'DisplayAccessEntryMembers

    Private Shared Sub DisplayPermissionMembers(ByVal
 keyContainerPermParam As _
        KeyContainerPermission, ByVal keyContainerPermAccEntryArrayParam()
 _
        As KeyContainerPermissionAccessEntry)
        Dim keyContainerPerm2 As KeyContainerPermission
 = keyContainerPermParam
        Dim keyContainerPermAccEntryArray As
 KeyContainerPermissionAccessEntry() = _
            keyContainerPermAccEntryArrayParam
        ' Display the KeyContainerPermission properties.
        Console.WriteLine(vbLf + "Flags value is "
 + keyContainerPerm2.Flags.ToString())
        Dim keyContainerPerm3 As KeyContainerPermission
 = _
            CType(keyContainerPerm2.Copy(), KeyContainerPermission)
        Console.WriteLine("Is the copy equal to the original?
 " + _
            keyContainerPerm3.Equals(keyContainerPerm2).ToString())
        ' Perform an XML roundtrip.
        keyContainerPerm3.FromXml(keyContainerPerm2.ToXml())
        Console.WriteLine("Was the XML roundtrip successful? "
 + _
            keyContainerPerm3.Equals(keyContainerPerm2).ToString())
        Dim keyContainerPerm4 As New
 KeyContainerPermission(KeyContainerPermissionFlags.Open, _
            keyContainerPermAccEntryArray)
        Dim keyContainerPerm5 As KeyContainerPermission
 = _
            CType(keyContainerPerm2.Intersect(keyContainerPerm4), KeyContainerPermission)
        Console.WriteLine("Flags value after the intersection
 is " + _
            keyContainerPerm5.Flags.ToString())
        keyContainerPerm5 = CType(keyContainerPerm2.Union(keyContainerPerm4), _
            KeyContainerPermission)
        Console.WriteLine("Flags value after the union is "
 + _
            keyContainerPerm5.Flags.ToString())
        Console.WriteLine("Is one permission a subset of the other?
 " + _
            keyContainerPerm4.IsSubsetOf(keyContainerPerm2).ToString())

    End Sub 'DisplayPermissionMembers

    Private Shared Sub GenKey_SaveInContainer(ByVal
 containerName As String)
        ' Create the CspParameters object and set the key container
 
        ' name used to store the RSA key pair.
        cspParams = New CspParameters()

        cspParams.KeyContainerName = containerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container identified by the containerName parameter.
        rsa = New RSACryptoServiceProvider(cspParams)

        ' Display the key information to the console.
        Console.WriteLine(vbLf + "Key added to container: "
 + vbLf + "  {0}", _
            rsa.ToXmlString(True))

    End Sub 'GenKey_SaveInContainer

    Private Shared Sub GetKeyFromContainer(ByVal
 containerName As String)
        Try
            cspParams = New CspParameters()
            cspParams.KeyContainerName = containerName

            ' Create a new instance of RSACryptoServiceProvider that
 accesses
            ' the key container identified by the containerName parameter.
            ' If the key container does not exist, a new one is created.
            rsa = New RSACryptoServiceProvider(cspParams)

            ' Use the rsa object to access the key. 
            ' Display the key information to the console.
            Console.WriteLine(vbLf + "Key retrieved from container
 : " + _
                vbLf + " {0}", rsa.ToXmlString(True))
            Console.WriteLine("KeycontainerName is "
 + rsa.CspKeyContainerInfo.KeyContainerName)
            Console.WriteLine("ProviderName is " +
 rsa.CspKeyContainerInfo.ProviderName)
            Console.WriteLine("ProviderType is " +
 IIf(1 = _
                rsa.CspKeyContainerInfo.ProviderType, "PROV_RSA_FULL",
 _
                rsa.CspKeyContainerInfo.ProviderType.ToString()))
        Catch e As Exception
            Console.WriteLine("Exception thrown:  "
 + e.Message)
        End Try

    End Sub 'GetKeyFromContainer

    Private Shared Sub DeleteKeyContainer(ByVal
 containerName As String)
        ' Create the CspParameters object and set the key container
 
        ' name used to store the RSA key pair.
        cspParams = New CspParameters()
        cspParams.KeyContainerName = containerName

        ' Create a new instance of RSACryptoServiceProvider that accesses
        ' the key container.
        rsa = New RSACryptoServiceProvider(cspParams)

        ' Do not persist the key entry, effectively deleting the key.
        rsa.PersistKeyInCsp = False

        ' Call Clear to release the key container resources.
        rsa.Clear()
        Console.WriteLine(vbLf + "Key container released.")

    End Sub 'DeleteKeyContainer

    Private Shared Sub DenyOpen()
        Try
            ' Create a KeyContainerPermission with the right to open
 the key container.
            Dim keyContainerPerm As New
 KeyContainerPermission(KeyContainerPermissionFlags.Open)

            ' Demonstrate the results of a deny for an open action.
            keyContainerPerm.Deny()

            ' The next line causes an exception to be thrown when the
 infrastructure code attempts 
            ' to open the key container.
            Dim info As New
 CspKeyContainerInfo(cspParams)
        Catch e As Exception
            Console.WriteLine("Expected exception thrown: "
 + e.Message)
        End Try

        ' Revert the deny.
        CodeAccessPermission.RevertDeny()

    End Sub 'DenyOpen

    Private Shared Sub DeleteContainer()
        Try
            ' Create a KeyContainerPermission with the right to create
 a key container.
            Dim keyContainerPerm As New
 KeyContainerPermission(KeyContainerPermissionFlags.Create)

            ' Deny the ability to create a key container.
            ' This deny is used to show the key container has been successfully
 deleted.
            keyContainerPerm.Deny()

            ' Retrieve the key from the container.
            ' This code executes successfully because the key container
 already exists.
            ' The deny for permission to create a key container does
 not affect this method call.
            GetKeyFromContainer("MyKeyContainer")

            ' Delete the key and the container.
            DeleteKeyContainer("MyKeyContainer")

            ' Attempt to obtain the key from the deleted key container.
            ' This time the method call results in an exception because
 of
            ' an attempt to create a new key container.
            Console.WriteLine(vbLf + _
                "Attempt to create a new key container with create
 permission denied.")
            GetKeyFromContainer("MyKeyContainer")
        Catch e As CryptographicException
            Console.WriteLine("Expected exception thrown: "
 + e.Message)
        End Try

    End Sub 'DeleteContainer
End Class 'KeyContainerPermissionDemo
using System;
using System.Security;
using System.Security.Permissions;
using System.Security.Cryptography;

namespace KeyContainerPermissionDemo
{
    public class KeyContainerPermissionDemo
    {
        private static CspParameters cspParams
 = new CspParameters();
        private static RSACryptoServiceProvider
 rsa = 
            new RSACryptoServiceProvider();
        private static string
 providerName;
        private static int
 providerType;
        private static string
 myKeyContainerName;
        
        // Create three KeyContainerPermissionAccessEntry objects, 
        // each with a different constructor.
        private static KeyContainerPermissionAccessEntry
 
            keyContainerPermAccEntry1 = new KeyContainerPermissionAccessEntry(
            "MyKeyContainer", KeyContainerPermissionFlags.Create);
        private static KeyContainerPermissionAccessEntry
 
            keyContainerPermAccEntry2 = new KeyContainerPermissionAccessEntry(
            cspParams, KeyContainerPermissionFlags.Open);
        private static KeyContainerPermissionAccessEntry
 
            keyContainerPermAccEntry3 = new KeyContainerPermissionAccessEntry(
            "Machine", providerName, providerType, myKeyContainerName,
 1, 
            KeyContainerPermissionFlags.Open);

        public static int
 Main()
        {
            try
            {
                // Create a key container for use in the sample.
                GenKey_SaveInContainer("MyKeyContainer");
                
                // Initialize property values for creating a 
                // KeyContainerPermissionAccessEntry object.
                myKeyContainerName = rsa.CspKeyContainerInfo.KeyContainerName;
                providerName = rsa.CspKeyContainerInfo.ProviderName;
                providerType = rsa.CspKeyContainerInfo.ProviderType;
                cspParams.KeyContainerName = myKeyContainerName;
                cspParams.ProviderName = providerName;
                cspParams.ProviderType = providerType;

                // Display the KeyContainerPermissionAccessEntry properties
  
                // using the third KeyContainerPermissionAccessEntry
 object.
                DisplayAccessEntryMembers();
                
                // Add access entry objects to a key container permission.
                KeyContainerPermission keyContainerPerm1 = 
                    new KeyContainerPermission(PermissionState.Unrestricted);
                Console.WriteLine("Is the permission unrestricted? " +
 
                    keyContainerPerm1.IsUnrestricted());
                keyContainerPerm1.AccessEntries.Add(keyContainerPermAccEntry1);
                keyContainerPerm1.AccessEntries.Add(keyContainerPermAccEntry2);

                // Display the permission.
                System.Console.WriteLine(keyContainerPerm1.ToXml().ToString());

                // Create an array of KeyContainerPermissionAccessEntry
 objects
                KeyContainerPermissionAccessEntry[] keyContainerPermAccEntryArray
 
                    = { keyContainerPermAccEntry1, keyContainerPermAccEntry2 };

                // Create a new KeyContainerPermission using the array.
                KeyContainerPermission keyContainerPerm2 = 
                    new KeyContainerPermission(
                    KeyContainerPermissionFlags.AllFlags,
                    keyContainerPermAccEntryArray);

                DisplayPermissionMembers(
                    keyContainerPerm2, keyContainerPermAccEntryArray);

                // Demonstrate the effect of a deny for opening a key
 container.
                DenyOpen();

                // Demonstrate the deletion of a key container.    
       
                DeleteContainer();

                Console.WriteLine("Press the Enter key to exit.");
                Console.Read();
                return 0;
            // Close the current try block that did not expect an exception.
            }
            catch (Exception e)
            {
                Console.WriteLine("Unexpected exception thrown:  " + e.Message);
                return 0;
            }
        }

        private static void
 DisplayAccessEntryMembers()
        {
            Console.WriteLine("\nKeycontainerName is " + 
                keyContainerPermAccEntry3.KeyContainerName);
            Console.WriteLine("KeySpec is " + (1 == 
                keyContainerPermAccEntry3.KeySpec ? "AT_KEYEXCHANGE " :
 
                "AT_SIGNATURE"));
            Console.WriteLine("KeyStore is " + 
                keyContainerPermAccEntry3.KeyStore);
            Console.WriteLine("ProviderName is " + 
                keyContainerPermAccEntry3.ProviderName);
            Console.WriteLine("ProviderType is " + (1 == 
                keyContainerPermAccEntry3.ProviderType ? "PROV_RSA_FULL"
 :
                keyContainerPermAccEntry3.ProviderType.ToString()));
            Console.WriteLine("Hashcode = " + 
                keyContainerPermAccEntry3.GetHashCode());
            Console.WriteLine(
                "Are the KeyContainerPermissionAccessEntry objects equal? "
 + 
                keyContainerPermAccEntry3.Equals(keyContainerPermAccEntry2));
        }

        private static void
 DisplayPermissionMembers(
            KeyContainerPermission keyContainerPermParam,
            KeyContainerPermissionAccessEntry[] 
            keyContainerPermAccEntryArrayParam)
        {
            KeyContainerPermission keyContainerPerm2 = keyContainerPermParam;
            KeyContainerPermissionAccessEntry[] keyContainerPermAccEntryArray = 
                keyContainerPermAccEntryArrayParam;

            // Display the KeyContainerPermission properties.
            Console.WriteLine("\nFlags value is " + 
                keyContainerPerm2.Flags.ToString());
            KeyContainerPermission keyContainerPerm3 = 
                (KeyContainerPermission)keyContainerPerm2.Copy();
            Console.WriteLine("Is the copy equal to the original? " + 
                keyContainerPerm3.Equals(keyContainerPerm2));

            // Perform an XML roundtrip.
            keyContainerPerm3.FromXml(keyContainerPerm2.ToXml());
            Console.WriteLine("Was the XML roundtrip successful? " + 
                keyContainerPerm3.Equals(keyContainerPerm2));
            KeyContainerPermission keyContainerPerm4 = 
                new KeyContainerPermission(KeyContainerPermissionFlags.Open
,
                keyContainerPermAccEntryArray);
            KeyContainerPermission keyContainerPerm5 = 
                (KeyContainerPermission)keyContainerPerm2.Intersect(
                keyContainerPerm4);
            Console.WriteLine("Flags value after the intersection is "
 + 
                keyContainerPerm5.Flags.ToString());
            keyContainerPerm5 = (KeyContainerPermission)keyContainerPerm2.Union(
                keyContainerPerm4);
            Console.WriteLine("Flags value after the union is " + 
                keyContainerPerm5.Flags.ToString());
            Console.WriteLine("Is one permission a subset of the other? "
 + 
                keyContainerPerm4.IsSubsetOf(keyContainerPerm2));
        }

        private static void
 GenKey_SaveInContainer(string containerName)
        {
            // Create the CspParameters object and set the key container
 
            // name used to store the RSA key pair.
            cspParams = new CspParameters();

            cspParams.KeyContainerName = containerName;

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

            // Display the key information to the console.
            Console.WriteLine("\nKey added to container: \n  {0}", 
                rsa.ToXmlString(true));
        }
        private static void
 GetKeyFromContainer(string containerName)
        {
            try
            {
                cspParams = new CspParameters();
                cspParams.KeyContainerName = containerName;

                // Create a new instance of RSACryptoServiceProvider that
 accesses
                // the key container identified by the containerName
 parameter.
                // If the key container does not exist, a new one is
 created.
                rsa = new RSACryptoServiceProvider(cspParams);

                // Use the rsa object to access the key. 
                // Display the key information to the console.
                Console.WriteLine("\nKey retrieved from container : \n {0}",
 
                    rsa.ToXmlString(true));
                Console.WriteLine("KeycontainerName is " + 
                    rsa.CspKeyContainerInfo.KeyContainerName);
                Console.WriteLine("ProviderName is " + 
                    rsa.CspKeyContainerInfo.ProviderName);
                Console.WriteLine("ProviderType is " + (1 == 
                    rsa.CspKeyContainerInfo.ProviderType ? "PROV_RSA_FULL"
 : 
                    rsa.CspKeyContainerInfo.ProviderType.ToString()));
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception thrown:  " + e.Message);
            }

        }
        private static void
 DeleteKeyContainer(string containerName)
        {
            // Create the CspParameters object and set the key container
 
            // name used to store the RSA key pair.

            cspParams = new CspParameters();
            cspParams.KeyContainerName = containerName;

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

            // Do not persist the key entry, effectively deleting the
 key.
            rsa.PersistKeyInCsp = false;

            // Call Clear to release the key container resources.
            rsa.Clear();
            Console.WriteLine("\nKey container released.");
        }
        private static void
 DenyOpen()
        {
            try
            {
                // Create a KeyContainerPermission with the right 
                // to open the key container.
                KeyContainerPermission keyContainerPerm = new
                     KeyContainerPermission(KeyContainerPermissionFlags.Open);

                // Demonstrate the results of a deny for an open action.
                keyContainerPerm.Deny();

                // The next line causes an exception to be thrown when
 the
                // infrastructure code attempts to open the key container.
                CspKeyContainerInfo info = new CspKeyContainerInfo(cspParams);
            }
            catch (Exception e)
            {
                Console.WriteLine("Expected exception thrown: " + e.Message);
            }

            // Revert the deny.
            CodeAccessPermission.RevertDeny();
        }
        private static void
 DeleteContainer()
        {
            try
            {
                // Create a KeyContainerPermission with the right to
 
                // create a key container.
                KeyContainerPermission keyContainerPerm = new
 
                    KeyContainerPermission(KeyContainerPermissionFlags.Create);

                // Deny the ability to create a key container.
                // This deny is used to show the key container has been
 
                // successfully deleted.
                keyContainerPerm.Deny();

                // Retrieve the key from the container.
                // This code executes successfully because the key 
                // container already exists.
                // The deny for permission to create a key container
 
                // does not affect this method call.
                GetKeyFromContainer("MyKeyContainer");

                // Delete the key and the container.
                DeleteKeyContainer("MyKeyContainer");

                // Attempt to obtain the key from the deleted key container.
                // This time the method call results in an exception
 because of
                // an attempt to create a new key container.
                Console.WriteLine("\nAttempt to create a new
 key container " +
                    "with create permission denied.");
                GetKeyFromContainer("MyKeyContainer");
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("Expected exception thrown: " + e.Message);
            }
        }
    }
}
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Cryptography;

public ref class KeyContainerPermissionDemo
{
private:
   static CspParameters^ cspParams = gcnew CspParameters;
   static RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider;
   static String^ providerName;
   static int providerType;
   static String^ myKeyContainerName;

   // Create three KeyContainerPermissionAccessEntry objects, each with
 a different constructor.
   static KeyContainerPermissionAccessEntry^ keyContainerPermAccEntry1
 = gcnew KeyContainerPermissionAccessEntry( "MyKeyContainer",KeyContainerPermissionFlags::Create
 );

   static KeyContainerPermissionAccessEntry^ keyContainerPermAccEntry2
 = gcnew KeyContainerPermissionAccessEntry( cspParams,KeyContainerPermissionFlags::Open
 );

   static KeyContainerPermissionAccessEntry^ keyContainerPermAccEntry3
 = gcnew KeyContainerPermissionAccessEntry( "Machine",providerName,providerType,myKeyContainerName,1,KeyContainerPermissionFlags::Open
 );

public:

   static int Main()
   {
      try
      {
         
         // Create a key container for use in the sample.
         GenKey_SaveInContainer( "MyKeyContainer" );
         
         // Initialize property values for creating a KeyContainerPermissionAccessEntry
 object.
         myKeyContainerName = rsa->CspKeyContainerInfo->KeyContainerName;
         providerName = rsa->CspKeyContainerInfo->ProviderName;
         providerType = rsa->CspKeyContainerInfo->ProviderType;
         cspParams->KeyContainerName = myKeyContainerName;
         cspParams->ProviderName = providerName;
         cspParams->ProviderType = providerType;
         
         // Display the KeyContainerPermissionAccessEntry properties
 using 
         // the third KeyContainerPermissionAccessEntry object.
         DisplayAccessEntryMembers();
         
         // Add access entry objects to a key container permission.
         KeyContainerPermission ^ keyContainerPerm1 = gcnew KeyContainerPermission(
 PermissionState::Unrestricted );
         Console::WriteLine( "Is the permission unrestricted? {0}", keyContainerPerm1->IsUnrestricted()
 );
         keyContainerPerm1->AccessEntries->Add( keyContainerPermAccEntry1 );
         keyContainerPerm1->AccessEntries->Add( keyContainerPermAccEntry2 );
         
         // Display the permission.
         System::Console::WriteLine( keyContainerPerm1->ToXml() );
         
         // Create an array of KeyContainerPermissionAccessEntry objects
         array<KeyContainerPermissionAccessEntry^>^keyContainerPermAccEntryArray
 = {keyContainerPermAccEntry1,keyContainerPermAccEntry2};
         
         // Create a new KeyContainerPermission using the array.
         KeyContainerPermission ^ keyContainerPerm2 = gcnew KeyContainerPermission(
 KeyContainerPermissionFlags::AllFlags,keyContainerPermAccEntryArray );
         
         DisplayPermissionMembers( keyContainerPerm2, keyContainerPermAccEntryArray
 );
         
         // Demonstrate the effect of a deny for opening a key container.
         DenyOpen();
         
         // Demonstrate the deletion of a key container.           
         DeleteContainer();
         Console::WriteLine( "Press the Enter key to exit." );
         Console::ReadKey();
         return 0;
         
         // Close the current try block that did not expect an exception.
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Unexpected exception thrown:  {0}", e->Message
 );
         return 0;
      }

   }


private:
   static void DisplayAccessEntryMembers()
   {
      
      Console::WriteLine( "\nKeycontainerName is {0}", keyContainerPermAccEntry3->KeyContainerName
 );
      
      Console::WriteLine( "KeySpec is {0}", (1 == keyContainerPermAccEntry3->KeySpec
 ? "AT_KEYEXCHANGE " : "AT_SIGNATURE") );
      
      Console::WriteLine( "KeyStore is {0}", keyContainerPermAccEntry3->KeyStore
 );
      
      Console::WriteLine( "ProviderName is {0}", keyContainerPermAccEntry3->ProviderName
 );
      
      Console::WriteLine( "ProviderType is {0}", (1 == keyContainerPermAccEntry3->ProviderType
 ? "PROV_RSA_FULL" : keyContainerPermAccEntry3->ProviderType.ToString())
 );
      
      Console::WriteLine( "Hashcode = {0}", keyContainerPermAccEntry3->GetHashCode()
 );
      
      Console::WriteLine( "Are the KeyContainerPermissionAccessEntry objects
 equal? {0}", keyContainerPermAccEntry3->Equals( keyContainerPermAccEntry2
 ) );
      
   }

   static void DisplayPermissionMembers( KeyContainerPermission
 ^ keyContainerPerm2, array<KeyContainerPermissionAccessEntry^>^keyContainerPermAccEntryArray
 )
   {
      
      // Display the KeyContainerPermission properties.
      Console::WriteLine( "\nFlags value is {0}", keyContainerPerm2->Flags
 );
      
      KeyContainerPermission ^ keyContainerPerm3 = dynamic_cast<KeyContainerPermission^>(keyContainerPerm2->Copy());
      Console::WriteLine( "Is the copy equal to the original? {0}", keyContainerPerm3->Equals(
 keyContainerPerm2 ) );
      
      // Perform an XML roundtrip.
      keyContainerPerm3->FromXml( keyContainerPerm2->ToXml() );
      Console::WriteLine( "Was the XML roundtrip successful? {0}", keyContainerPerm3->Equals(
 keyContainerPerm2 ) );
      
      KeyContainerPermission ^ keyContainerPerm4 = gcnew KeyContainerPermission(
 KeyContainerPermissionFlags::Open,keyContainerPermAccEntryArray );
      
      KeyContainerPermission ^ keyContainerPerm5 = dynamic_cast<KeyContainerPermission^>(keyContainerPerm2->Intersect(
 keyContainerPerm4 ));
      Console::WriteLine( "Flags value after the intersection is {0}",
 keyContainerPerm5->Flags );
      
      keyContainerPerm5 = dynamic_cast<KeyContainerPermission^>(keyContainerPerm2->Union(
 keyContainerPerm4 ));
      
      Console::WriteLine( "Flags value after the union is {0}", keyContainerPerm5->Flags
 );
      
      Console::WriteLine( "Is one permission a subset of the other? {0}",
 keyContainerPerm4->IsSubsetOf( keyContainerPerm2 ) );
      
   }

   static void GenKey_SaveInContainer( String^
 containerName )
   {
      
      // Create the CspParameters object and set the key container 
      // name used to store the RSA key pair.
      cspParams = gcnew CspParameters;
      cspParams->KeyContainerName = containerName;
      
      // Create a new instance of RSACryptoServiceProvider that accesses
      // the key container identified by the containerName parameter.
      rsa = gcnew RSACryptoServiceProvider( cspParams );
      
      // Display the key information to the console.
      Console::WriteLine( "\nKey added to container: \n  {0}", rsa->ToXmlString(
 true ) );
   }

   static void GetKeyFromContainer( String^
 containerName )
   {
      try
      {
         cspParams = gcnew CspParameters;
         cspParams->KeyContainerName = containerName;
         
         // Create a new instance of RSACryptoServiceProvider that accesses
         // the key container identified by the containerName parameter.
         // If the key container does not exist, a new one is created.
         rsa = gcnew RSACryptoServiceProvider( cspParams );
         
         // Use the rsa object to access the key. 
         // Display the key information to the console.
         Console::WriteLine( "\nKey retrieved from container : \n {0}",
 rsa->ToXmlString( true ) );
         Console::WriteLine( "KeycontainerName is {0}", rsa->CspKeyContainerInfo->KeyContainerName
 );
         Console::WriteLine( "ProviderName is {0}", rsa->CspKeyContainerInfo->ProviderName
 );
         Console::WriteLine( "ProviderType is {0}", (1 == rsa->CspKeyContainerInfo->ProviderType
 ? "PROV_RSA_FULL" : rsa->CspKeyContainerInfo->ProviderType.ToString())
 );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception thrown:  {0}", e->Message );
      }

   }

   static void DeleteKeyContainer( String^
 containerName )
   {
      
      // Create the CspParameters object and set the key container 
      // name used to store the RSA key pair.
      cspParams = gcnew CspParameters;
      cspParams->KeyContainerName = containerName;
      
      // Create a new instance of RSACryptoServiceProvider that accesses
      // the key container.
      rsa = gcnew RSACryptoServiceProvider( cspParams );
      
      // Do not persist the key entry, effectively deleting the key.
      rsa->PersistKeyInCsp = false;
      
      // Call Clear to release the key container resources.
      rsa->Clear();
      Console::WriteLine( "\nKey container released." );
   }

   static void DenyOpen()
   {
      try
      {
         
         // Create a KeyContainerPermission with the right to open the
 key container.
         KeyContainerPermission ^ keyContainerPerm = gcnew KeyContainerPermission(
 KeyContainerPermissionFlags::Open );
         
         // Demonstrate the results of a deny for an open action.
         keyContainerPerm->Deny();
         
         // The next line causes an exception to be thrown when the
 infrastructure code attempts 
         // to open the key container.
         CspKeyContainerInfo ^ info = gcnew CspKeyContainerInfo( cspParams );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Expected exception thrown: {0}", e->Message
 );
      }

      
      // Revert the deny.
      CodeAccessPermission::RevertDeny();
   }

   static void DeleteContainer()
   {
      try
      {
         
         // Create a KeyContainerPermission with the right to create
 a key container.
         KeyContainerPermission ^ keyContainerPerm = gcnew KeyContainerPermission(
 KeyContainerPermissionFlags::Create );
         
         // Deny the ability to create a key container.
         // This deny is used to show the key container has been successfully
 deleted.
         keyContainerPerm->Deny();
         
         // Retrieve the key from the container.
         // This code executes successfully because the key container
 already exists.
         // The deny for permission to create a key container does not
 affect this method call.
         GetKeyFromContainer( "MyKeyContainer" );
         
         // Delete the key and the container.
         DeleteKeyContainer( "MyKeyContainer" );
         
         // Attempt to obtain the key from the deleted key container.
         // This time the method call results in an exception because
 of
         // an attempt to create a new key container.
         Console::WriteLine( "\nAttempt to create a new key
 container with create permission denied." );
         GetKeyFromContainer( "MyKeyContainer" );
      }
      catch ( CryptographicException^ e ) 
      {
         Console::WriteLine( "Expected exception thrown: {0}", e->Message
 );
      }

   }

};

int main()
{
   return KeyContainerPermissionDemo::Main();
}

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

public class KeyContainerPermissionDemo
{
    private static CspParameters cspParams
 = new CspParameters();
    private static RSACryptoServiceProvider
 rsa = new RSACryptoServiceProvider();
    private static String providerName;
    private static int providerType;
    private static String myKeyContainerName;

    // Create three KeyContainerPermissionAccessEntry objects, 
    // each with a different constructor.
    private static KeyContainerPermissionAccessEntry
 
    keyContainerPermAccEntry1 = 
        new KeyContainerPermissionAccessEntry("MyKeyContainer",
 
            KeyContainerPermissionFlags.Create);

    private static KeyContainerPermissionAccessEntry
 keyContainerPermAccEntry2 = 
        new KeyContainerPermissionAccessEntry(cspParams, 
            KeyContainerPermissionFlags.Open);

    private static KeyContainerPermissionAccessEntry
 keyContainerPermAccEntry3 = 
        new KeyContainerPermissionAccessEntry("Machine",
 providerName, 
            providerType, myKeyContainerName, 1, KeyContainerPermissionFlags.Open);

    public static void main(String[]
 args)
    {
        try {
            // Create a key container for use in the sample.
            GenKey_SaveInContainer("MyKeyContainer");

            // Initialize property values for creating a 
            // KeyContainerPermissionAccessEntry object.
            myKeyContainerName = rsa.get_CspKeyContainerInfo().get_KeyContainerName();
            providerName = rsa.get_CspKeyContainerInfo().get_ProviderName();
            providerType = rsa.get_CspKeyContainerInfo().get_ProviderType();
            cspParams.KeyContainerName = myKeyContainerName;
            cspParams.ProviderName = providerName;
            cspParams.ProviderType = providerType;

            // Display the KeyContainerPermissionAccessEntry properties
 using 
            // the third KeyContainerPermissionAccessEntry object.
            DisplayAccessEntryMembers();

            // Add access entry objects to a key container permission.
            KeyContainerPermission keyContainerPerm1 = 
                new KeyContainerPermission(PermissionState.Unrestricted);
            Console.WriteLine(("Is the permission unrestricted? " 
                + System.Convert.ToString(keyContainerPerm1.IsUnrestricted())));
            keyContainerPerm1.get_AccessEntries().Add(keyContainerPermAccEntry1);
            keyContainerPerm1.get_AccessEntries().Add(keyContainerPermAccEntry2);

            // Display the permission.
            System.Console.WriteLine(keyContainerPerm1.ToXml().ToString());

            // Create an array of KeyContainerPermissionAccessEntry
 objects
            KeyContainerPermissionAccessEntry keyContainerPermAccEntryArray[] = 
 
                { keyContainerPermAccEntry1, keyContainerPermAccEntry2 };

            // Create a new KeyContainerPermission using the array.
            KeyContainerPermission keyContainerPerm2 = new KeyContainerPermission(
                KeyContainerPermissionFlags.AllFlags, 
                keyContainerPermAccEntryArray);

            DisplayPermissionMembers(keyContainerPerm2, keyContainerPermAccEntryArray);

            // Demonstrate the effect of a deny for opening a key container.
            DenyOpen();

            // Demonstrate the deletion of a key container.        
   
            DeleteContainer();
            Console.WriteLine("Press the Enter key to exit.");
            Console.ReadKey();
            return ;
            // Close the current try block that did not expect an exception.
        } 
        catch (System.Exception e) {
            Console.WriteLine(("Unexpected exception thrown:  " + e.get_Message()));
            return ;
        }
    } //main

    private static void
 DisplayAccessEntryMembers()
    {
        Console.WriteLine(("\nKeycontainerName is " + keyContainerPermAccEntry3.get_KeyContainerName()));

        Console.WriteLine(("KeySpec is " 
            + ((1 == keyContainerPermAccEntry3.get_KeySpec()) ? "AT_KEYEXCHANGE
 " : "AT_SIGNATURE")));

        Console.WriteLine(("KeyStore is " + keyContainerPermAccEntry3.get_KeyStore()));

        Console.WriteLine(("ProviderName is " + keyContainerPermAccEntry3.get_ProviderName()));

        Console.WriteLine(("ProviderType is " + ((1 == keyContainerPermAccEntry3.get_ProviderType())
 ?
            "PROV_RSA_FULL" : System.Convert.ToString(keyContainerPermAccEntry3.get_ProviderType()))));

        Console.WriteLine(("Hashcode = " + keyContainerPermAccEntry3.GetHashCode()));

        Console.WriteLine(("Are the KeyContainerPermissionAccessEntry objects
 equal? " 
            + System.Convert.ToString(keyContainerPermAccEntry3.Equals(keyContainerPermAccEntry2))));
    } //DisplayAccessEntryMembers

    private static void
 DisplayPermissionMembers(
        KeyContainerPermission keyContainerPermParam, 
        KeyContainerPermissionAccessEntry keyContainerPermAccEntryArrayParam[])
    {
        KeyContainerPermission keyContainerPerm2 = keyContainerPermParam;
        KeyContainerPermissionAccessEntry[] keyContainerPermAccEntryArray = keyContainerPermAccEntryArrayParam;

        // Display the KeyContainerPermission properties.
        Console.WriteLine(("\nFlags value is " + keyContainerPerm2.get_Flags().ToString()));

        KeyContainerPermission keyContainerPerm3 = (KeyContainerPermission)(keyContainerPerm2.Copy());

        Console.WriteLine(("Is the copy equal to the original? " + System.Convert.ToString(
            keyContainerPerm3.Equals(keyContainerPerm2))));

        // Perform an XML roundtrip.
        keyContainerPerm3.FromXml(keyContainerPerm2.ToXml());
        Console.WriteLine(("Was the XML roundtrip successful? " + System.Convert.ToString(
            keyContainerPerm3.Equals(keyContainerPerm2))));

        KeyContainerPermission keyContainerPerm4 = new KeyContainerPermission(
            KeyContainerPermissionFlags.Open, 
            keyContainerPermAccEntryArray);

        KeyContainerPermission keyContainerPerm5 = ((KeyContainerPermission)(
            keyContainerPerm2.Intersect(keyContainerPerm4)));
        Console.WriteLine(("Flags value after the intersection is "
            + keyContainerPerm5.get_Flags().ToString()));

        keyContainerPerm5 = ((KeyContainerPermission)(keyContainerPerm2.Union(keyContainerPerm4)));
        Console.WriteLine(("Flags value after the union is " 
            + keyContainerPerm5.get_Flags().ToString()));

        Console.WriteLine(("Is one permission a subset of the other? "
 + System.Convert.ToString(
            keyContainerPerm4.IsSubsetOf(keyContainerPerm2))));
    } //DisplayPermissionMembers
    
    private static void
 GenKey_SaveInContainer(String containerName)
    {
        // Create the CspParameters object and set the key container
 
        // name used to store the RSA key pair.
        cspParams = new CspParameters();
        cspParams.KeyContainerName = containerName;

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

        // Display the key information to the console.
        Console.WriteLine("\nKey added to container: \n  {0}", rsa.ToXmlString(true));
    } //GenKey_SaveInContainer

    private static void
 GetKeyFromContainer(String containerName)
    {
        try {
            cspParams = new CspParameters();
            cspParams.KeyContainerName = containerName;

            // Create a new instance of RSACryptoServiceProvider that
 accesses
            // the key container identified by the containerName parameter.
            // If the key container does not exist, a new one is created.
            rsa = new RSACryptoServiceProvider(cspParams);

            // Use the rsa object to access the key. 
            // Display the key information to the console.
            Console.WriteLine("\nKey retrieved from container : \n {0}",
 
                rsa.ToXmlString(true));
            Console.WriteLine(("KeycontainerName is " 
                + rsa.get_CspKeyContainerInfo().get_KeyContainerName()));
            Console.WriteLine(("ProviderName is " 
                + rsa.get_CspKeyContainerInfo().get_ProviderName()));
            Console.WriteLine(("ProviderType is " + ((1 == rsa.get_CspKeyContainerInfo().get_ProviderType())
 ? 
                "PROV_RSA_FULL" : System.Convert.ToString(rsa.get_CspKeyContainerInfo().get_ProviderType()))));
        }
        catch (System.Exception e) {
            Console.WriteLine(("Exception thrown:  " + e.get_Message()));
        }
    } //GetKeyFromContainer

    private static void
 DeleteKeyContainer(String containerName)
    {
        // Create the CspParameters object and set the key container
 
        // name used to store the RSA key pair.
        cspParams = new CspParameters();
        cspParams.KeyContainerName = containerName;

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

        // Do not persist the key entry, effectively deleting the key.
        rsa.set_PersistKeyInCsp(false);

        // Call Clear to release the key container resources.
        rsa.Clear();
        Console.WriteLine("\nKey container released.");
    } //DeleteKeyContainer

    private static void
 DenyOpen()
    {
        try {
            // Create a KeyContainerPermission with the 
            // right to open the key container.
            KeyContainerPermission keyContainerPerm = new KeyContainerPermission(KeyContainerPermissionFlags.Open);

            // Demonstrate the results of a deny for an open action.
            keyContainerPerm.Deny();

            // The next line causes an exception to be thrown when the
 
            // infrastructure code attempts 
            // to open the key container.
            CspKeyContainerInfo info = new CspKeyContainerInfo(cspParams);
        }
        catch (System.Exception e) {
            Console.WriteLine(("Expected exception thrown: "  + e.get_Message()));
        }

        // Revert the deny.
        CodeAccessPermission.RevertDeny();
    } //DenyOpen

    private static void
 DeleteContainer()
    {
        try {
            // Create a KeyContainerPermission with the right to create
 
            // a key container.
            KeyContainerPermission keyContainerPerm = 
                new KeyContainerPermission(KeyContainerPermissionFlags.Create);

            // Deny the ability to create a key container.
            // This deny is used to show the key container has
            // been successfully deleted.
            keyContainerPerm.Deny();

            // Retrieve the key from the container.
            // This code executes successfully because the key container
 
            // already exists.
            // The deny for permission to create a key container does
 not 
            // affect this method call.
            GetKeyFromContainer("MyKeyContainer");

            // Delete the key and the container.
            DeleteKeyContainer("MyKeyContainer");

            // Attempt to obtain the key from the deleted key container.
            // This time the method call results in an exception because
 of
            // an attempt to create a new key container.
            Console.WriteLine("\nAttempt to create a new
 key container "+ "with create permission denied.");
            GetKeyFromContainer("MyKeyContainer");
        }
        catch (CryptographicException e) {
            Console.WriteLine(("Expected exception thrown: " + e.get_Message()));
        }
    } //DeleteContainer
} //KeyContainerPermissionDemo   
継承階層継承階層
System.Object
   System.Security.CodeAccessPermission
    System.Security.Permissions.KeyContainerPermission
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
KeyContainerPermission メンバ
System.Security.Permissions 名前空間
KeyContainerPermissionAttribute
KeyContainerPermissionAccessEntry
その他の技術情報
アクセス許可
アクセス許可要求



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「KeyContainerPermission クラス」の関連用語

KeyContainerPermission クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



KeyContainerPermission クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS