KeyContainerPermissionAttribute Class
Allows security actions for KeyContainerPermission to be applied to code using declarative security. This class cannot be inherited.
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SerializableAttribute> _ <AttributeUsageAttribute(AttributeTargets.Assembly Or AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Constructor Or AttributeTargets.Method, AllowMultiple := True, _ Inherited := False)> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class KeyContainerPermissionAttribute _ Inherits CodeAccessSecurityAttribute 'Usage Dim instance As KeyContainerPermissionAttribute
The scope of the declaration that is allowed depends on the SecurityAction value that is used.
The security information declared by a security attribute is stored in the metadata of the attribute target and is accessed by the system at run time. KeyContainerPermissionAttribute is used only for declarative security. For imperative security, use the KeyContainerPermission class.
The following code example shows the use of members of the KeyContainerPermissionAttribute class.
' 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. 'Imports System 'Imports System.Security 'Imports System.Security.Permissions 'Imports System.Security.Cryptography 'Imports KeyContainerPermissionAttributeDemo 'Class AttributeTest ' ' Tests the KeyContainerPermissionAttribute. ' <STAThread()> _ ' Shared Sub Main() ' Dim cspParams As New CspParameters() ' cspParams.KeyContainerName = "MyKeyContainer" ' Dim rsa As New RSACryptoServiceProvider(cspParams) ' ' Display the key information to the console. ' Console.WriteLine("Key added to container: " + vbLf + " {0}", rsa.ToXmlString(True)) ' Dim keyCU As 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() ' End Sub 'Main 'End Class 'AttributeTest Imports System Imports System.Security Imports System.Security.Permissions Imports 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 Sub New() End Sub 'New ' The following method throws an exception if the containerName parameter identifies a ' new container. Otherwise the method executes correctly. Public Sub GenKey_SaveInContainer(ByVal containerName As String) Try ' Create the CspParameters object and set the name of the key ' container used to store the RSA key pair. Dim cspParams As New CspParameters() cspParams.KeyContainerName = containerName ' Create a new instance of RSACryptoServiceProvider that accesses ' the key container specified by the containerName parameter. Dim rsa As New RSACryptoServiceProvider(cspParams) ' Display the key information to the console. Console.WriteLine("Key added to container: " + vbLf + _ " {0}", rsa.ToXmlString(True)) Catch e As Exception Console.WriteLine("Exception thrown: " + e.Message) End Try End Sub 'GenKey_SaveInContainer Public Sub DeleteKeyFromContainer(ByVal containerName As String) 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. Dim cspParams As New CspParameters() cspParams.KeyContainerName = containerName ' Create a new instance of RSACryptoServiceProvider that accesses ' the key container MyKeycontainerName. Dim rsa As 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 e As Exception Console.WriteLine("Exception thrown when deleting key: " + e.Message) End Try End Sub 'DeleteKeyFromContainer End Class 'KeyContainerUtil End Namespace
System.Attribute
System.Security.Permissions.SecurityAttribute
System.Security.Permissions.CodeAccessSecurityAttribute
System.Security.Permissions.KeyContainerPermissionAttribute
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.