Imports System
Imports System.Data
Imports System.Security
Imports System.Security.Permissions
Imports System.Security.Policy
Imports System.Reflection
Imports System.Runtime.Serialization
<Assembly: KeyContainerPermissionAttribute(SecurityAction.RequestRefuse, _
Flags:=KeyContainerPermissionFlags.Import)>
Class EntryPoint
<STAThread()> _
Shared Sub Main()
Dim eP As New EntryPoint()
eP.RunCode()
Console.WriteLine("Press the ENTER key to exit.")
Console.Read()
End Sub 'Main
Sub RunCode()
Try
' Deny a permission.
Dim kCP1 As New KeyContainerPermission( _
KeyContainerPermissionFlags.Decrypt)
kCP1.Deny()
' Demand the denied permission and display the
' exception properties.
Display("Demanding a denied permission. " & vbLf & vbLf)
DemandDeniedPermission()
Display("************************************************" & vbLf)
CodeAccessPermission.RevertDeny()
' Demand the permission refused in the
' assembly-level attribute.
Display("Demanding a refused permission. " & vbLf & vbLf)
DemandRefusedPermission()
Display("************************************************" & vbLf)
' Demand the permission implicitly refused through a
' PermitOnly attribute. Permit only the permission that
' will cause the failure and the security permissions
' necessary to display the results of the failure.
Dim permitOnly As New PermissionSet(PermissionState.None)
permitOnly.AddPermission( _
New KeyContainerPermission(KeyContainerPermissionFlags.Import))
permitOnly.AddPermission(New SecurityPermission( _
SecurityPermissionFlag.ControlEvidence Or _
SecurityPermissionFlag.ControlPolicy Or _
SecurityPermissionFlag.SerializationFormatter))
permitOnly.PermitOnly()
Display( _
"Demanding an implicitly refused permission. " & vbLf & vbLf)
DemandPermitOnly()
Catch sE As Exception
Display("************************************************" & vbLf)
Display("Displaying an exception using the ToString method: ")
Display(sE.ToString())
End Try
End Sub 'RunCode
Sub DemandDeniedPermission()
Try
Dim kCP As New KeyContainerPermission( _
KeyContainerPermissionFlags.Decrypt)
kCP.Demand()
Catch sE As SecurityException
Display("The denied permission is: " & _
CType(sE.DenySetInstance, PermissionSet).ToString())
Display("The demanded permission is: " & sE.Demanded.ToString())
Display("The security action is: " & sE.Action.ToString())
Display("The method is: " & sE.Method.ToString())
Display( _
"The permission state at the time of the exception was: " & _
sE.PermissionState)
Display("The permission that failed was: " & _
CType(sE.FirstPermissionThatFailed, _
IPermission).ToXml().ToString())
Display("The permission type is: " & sE.PermissionType.ToString())
Display("Demonstrating the use of the GetObjectData method.")
Dim si As New SerializationInfo( _
GetType(EntryPoint), New FormatterConverter())
sE.GetObjectData(si, _
New StreamingContext(StreamingContextStates.All))
Display("The FirstPermissionThatFailed from the " & _
"call to GetObjectData is: ")
Display(si.GetString("FirstPermissionThatFailed"))
End Try
End Sub 'DemandDeniedPermission
Sub DemandRefusedPermission()
Try
Dim kCP As New KeyContainerPermission( _
KeyContainerPermissionFlags.Import)
kCP.Demand()
Catch sE As SecurityException
Display("The refused permission set is: " & _
sE.RefusedSet.ToString())
Display("The exception message is: " & sE.Message)
Display("The failed assembly is: " & _
sE.FailedAssemblyInfo.EscapedCodeBase)
Display("The granted set is: " & vbLf & sE.GrantedSet)
Display("The permission that failed is: " & CType( _
sE.FirstPermissionThatFailed, IPermission).ToXml().ToString())
Display("The permission type is: " & sE.PermissionType.ToString())
Display("The source is: " & sE.Source)
End Try
End Sub 'DemandRefusedPermission
Sub DemandPermitOnly()
Try
Dim kCP As New KeyContainerPermission( _
KeyContainerPermissionFlags.Decrypt)
kCP.Demand()
Catch sE As SecurityException
Display("The permitted permission is: " & _
CType(sE.PermitOnlySetInstance, PermissionSet).ToString())
Display("The demanded permission is: " & sE.Demanded.ToString())
Display("The security action is: " & sE.Action.ToString())
Display("The method is: " & sE.Method.ToString())
Display("The permission state at the time of the exception was: " & _
sE.PermissionState)
Display("The permission that failed was: " & CType( _
sE.FirstPermissionThatFailed, IPermission).ToXml().ToString())
Display("The permission type is: " & sE.PermissionType.ToString())
' Demonstrate the SecurityException constructor by
' throwing the exception again.
Display("Rethrowing the exception thrown as a result of a " & _
"PermitOnly security action.")
Throw New SecurityException(sE.Message, sE.DenySetInstance, _
sE.PermitOnlySetInstance, sE.Method, sE.Demanded, _
CType(sE.FirstPermissionThatFailed, IPermission))
End Try
End Sub 'DemandPermitOnly
Sub Display(ByVal line As String)
Console.WriteLine(line)
End Sub 'Display
End Class 'EntryPoint
using System;
using System.Data;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Reflection;
using System.Runtime.Serialization;
[assembly: KeyContainerPermissionAttribute(SecurityAction.RequestRefuse,
Flags = KeyContainerPermissionFlags.Import)]
namespace TestForm
{
class EntryPoint
{
[STAThread]
static void Main()
{
EntryPoint eP = new EntryPoint();
eP.RunCode();
Console.WriteLine("Press the ENTER key to exit.");
Console.Read();
}
void RunCode()
{
try
{
// Deny a permission.
KeyContainerPermission kCP1 = new KeyContainerPermission(
KeyContainerPermissionFlags.Decrypt);
kCP1.Deny();
// Demand the denied permission and display the
// exception properties.
Display("Demanding a denied permission. \n\n");
DemandDeniedPermission();
Display("************************************************\n");
CodeAccessPermission.RevertDeny();
// Demand the permission refused in the
// assembly-level attribute.
Display("Demanding a refused permission. \n\n");
DemandRefusedPermission();
Display("************************************************\n");
// Demand the permission implicitly refused through a
// PermitOnly attribute. Permit only the permission that
// will cause the failure and the security permissions
// necessary to display the results of the failure.
PermissionSet permitOnly = new PermissionSet(
PermissionState.None);
permitOnly.AddPermission(new KeyContainerPermission(
KeyContainerPermissionFlags.Import));
permitOnly.AddPermission(new SecurityPermission(
SecurityPermissionFlag.ControlEvidence |
SecurityPermissionFlag.ControlPolicy |
SecurityPermissionFlag.SerializationFormatter));
permitOnly.PermitOnly();
Display("Demanding an implicitly refused permission. \n\n");
DemandPermitOnly();
}
catch (Exception sE)
{
Display("************************************************\n");
Display("Displaying an exception using the ToString method: ");
Display(sE.ToString());
}
}
void DemandDeniedPermission()
{
try
{
KeyContainerPermission kCP = new KeyContainerPermission(
KeyContainerPermissionFlags.Decrypt);
kCP.Demand();
}
catch (SecurityException sE)
{
Display("The denied permission is: " +
((PermissionSet)sE.DenySetInstance).ToString());
Display("The demanded permission is: " +
sE.Demanded.ToString());
Display("The security action is: " + sE.Action.ToString());
Display("The method is: " + sE.Method);
Display(
"The permission state at the time of the exception was: " +
sE.PermissionState);
Display("The permission that failed was: " +
(IPermission)sE.FirstPermissionThatFailed);
Display("The permission type is: " +
sE.PermissionType.ToString());
Display("Demonstrating the use of the GetObjectData method.");
SerializationInfo si = new SerializationInfo(
typeof(EntryPoint), new FormatterConverter());
sE.GetObjectData(si,
new StreamingContext(StreamingContextStates.All));
Display("The FirstPermissionThatFailed from the " +
"call to GetObjectData is: ");
Display(si.GetString("FirstPermissionThatFailed"));
}
}
void DemandRefusedPermission()
{
try
{
KeyContainerPermission kCP = new KeyContainerPermission(
KeyContainerPermissionFlags.Import);
kCP.Demand();
}
catch (SecurityException sE)
{
Display("The refused permission set is: " +
(sE.RefusedSet).ToString());
Display("The exception message is: " + sE.Message);
Display("The failed assembly is: " +
sE.FailedAssemblyInfo.EscapedCodeBase);
Display("The granted set is: \n" + sE.GrantedSet);
Display("The permission that failed is: " +
sE.FirstPermissionThatFailed);
Display("The permission type is: " +
sE.PermissionType.ToString());
Display("The source is: " + sE.Source);
}
}
void DemandPermitOnly()
{
try
{
KeyContainerPermission kCP = new KeyContainerPermission(
KeyContainerPermissionFlags.Decrypt);
kCP.Demand();
}
catch (SecurityException sE)
{
Display("The permitted permission is: " +
((PermissionSet)sE.PermitOnlySetInstance).ToString());
Display("The demanded permission is: " +
sE.Demanded.ToString());
Display("The security action is: " + sE.Action.ToString());
Display("The method is: " + sE.Method.ToString());
Display(
"The permission state at the time of the exception was: " +
sE.PermissionState);
Display("The permission that failed was: " +
(IPermission)sE.FirstPermissionThatFailed);
Display("The permission type is: " +
sE.PermissionType.ToString());
//Demonstrate the SecurityException constructor by
// throwing the exception again.
Display("Rethrowing the exception thrown as a result of a " +
"PermitOnly security action.");
throw new SecurityException(sE.Message, sE.DenySetInstance,
sE.PermitOnlySetInstance, sE.Method, sE.Demanded,
(IPermission)sE.FirstPermissionThatFailed);
}
}
void Display(string line)
{
Console.WriteLine(line);
}
}
}
using namespace System;
using namespace System::Data;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Policy;
using namespace System::Reflection;
using namespace System::Runtime::Serialization;
namespace SecurityExceptionSample
{
[assembly:KeyContainerPermissionAttribute(
SecurityAction::RequestRefuse,
Flags=KeyContainerPermissionFlags::Import)];
ref class TestSecurityException
{
public:
void MakeTest()
{
try
{
// Deny a permission.
KeyContainerPermission^ keyContainerDecryptPermission =
gcnew KeyContainerPermission(
KeyContainerPermissionFlags::Decrypt);
keyContainerDecryptPermission->Deny();
// Demand the denied permission and display
// the exception properties.
Display("Demanding a denied permission. \n\n");
DemandDeniedPermission();
Display("*******************************************"
"**************\n");
CodeAccessPermission::RevertDeny();
// Demand the permission refused
// in the assembly-level attribute.
Display("Demanding a refused permission. \n\n");
DemandRefusedPermission();
Display("*******************************************"
"**************\n");
// Demand the permission implicitly refused through a
// PermitOnly attribute. Permit only the permission that
// will cause the failure and the security permissions
// necessary to display the results of the failure.
PermissionSet^ permitOnlySet = gcnew PermissionSet(
PermissionState::None);
permitOnlySet->AddPermission(gcnew KeyContainerPermission(
KeyContainerPermissionFlags::Import));
permitOnlySet->AddPermission(gcnew SecurityPermission(
SecurityPermissionFlag::ControlEvidence |
SecurityPermissionFlag::ControlPolicy |
SecurityPermissionFlag::SerializationFormatter));
permitOnlySet->PermitOnly();
Display("Demanding an implicitly refused permission. \n\n");
DemandPermitOnly();
}
catch (SecurityException^ exception)
{
Display("*******************************************"
"**************\n");
Display("Displaying an exception using the ToString "
"method: ");
Display(exception->ToString());
}
}
private:
void DemandDeniedPermission()
{
try
{
KeyContainerPermission^ keyContainerDecryptPermission =
gcnew KeyContainerPermission(
KeyContainerPermissionFlags::Decrypt);
keyContainerDecryptPermission->Demand();
}
catch (SecurityException^ exception)
{
Display("The denied permission is: {0}",
exception->DenySetInstance);
Display("The demanded permission is: {0}",
exception->Demanded);
Display("The security action is: {0}",
exception->Action);
Display("The method is: {0}", exception->Method);
Display("The permission state at the time "
"of the exception was: {0}",
exception->PermissionState);
Display("The permission that failed was: {0}",
exception->FirstPermissionThatFailed);
Display("The permission type is: {0}",
exception->PermissionType);
Display("Demonstrating the use of the GetObjectData "
"method.");
SerializationInfo^ entryPointSerializatonInfo =
gcnew SerializationInfo(TestSecurityException::typeid,
gcnew FormatterConverter);
exception->GetObjectData(entryPointSerializatonInfo,
*gcnew StreamingContext(StreamingContextStates::All));
Display("The FirstPermissionThatFailed from the call"
" to GetObjectData is: ");
Display(entryPointSerializatonInfo->GetString(
"FirstPermissionThatFailed"));
}
}
void DemandRefusedPermission()
{
try
{
KeyContainerPermission^ keyContainerImportPermission =
gcnew KeyContainerPermission(
KeyContainerPermissionFlags::Import);
keyContainerImportPermission->Demand();
}
catch (SecurityException^ exception)
{
Display("The refused permission set is: {0}",
exception->RefusedSet);
Display("The exception message is: {0}",
exception->Message);
Display("The failed assembly is: {0}",
exception->FailedAssemblyInfo->EscapedCodeBase);
Display("The granted set is: \n{0}",
exception->GrantedSet);
Display("The permission that failed is: {0}",
exception->FirstPermissionThatFailed);
Display("The permission type is: {0}",
exception->PermissionType);
Display("The source is: {0}", exception->Source);
}
}
void DemandPermitOnly()
{
try
{
KeyContainerPermission^ keyContainerDecryptPermission =
gcnew KeyContainerPermission(
KeyContainerPermissionFlags::Decrypt);
keyContainerDecryptPermission->Demand();
}
catch (SecurityException^ exception)
{
Display("The permitted permission is: {0}",
exception->PermitOnlySetInstance);
Display("The demanded permission is: {0}",
exception->Demanded);
Display("The security action is: {0}",
exception->Action);
Display("The method is: {0}", exception->Method);
Display("The permission state at the time of the "
"exception was: {0}", exception->PermissionState);
Display("The permission that failed was: {0}",
exception->FirstPermissionThatFailed);
Display("The permission type is: {0}",
exception->PermissionType);
// Demonstrate the SecurityException constructor
// by throwing the exception again.
Display("Rethrowing the exception thrown as a "
"result of a PermitOnly security action.");
throw gcnew SecurityException(exception->Message,
exception->DenySetInstance,
exception->PermitOnlySetInstance,
exception->Method, exception->Demanded,
exception->FirstPermissionThatFailed);
}
}
void Display(String^ line)
{
Console::WriteLine(line);
}
void Display(String^ format, Object^ arg)
{
Console::WriteLine(format, arg);
}
};
}
using namespace SecurityExceptionSample;
int main()
{
TestSecurityException^ test = gcnew TestSecurityException;
test->MakeTest();
Console::WriteLine("Press the enter key to exit.");
Console::Read();
}