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();
}