using namespace System;
using namespace System::Security;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;
using namespace System::Security::Permissions;
using namespace System::Diagnostics;
public ref class Demo
{
public:
// Main method.
void Run()
{
try
{
Copy_EqualsDemo();
To_FromXmlDemo();
GetTypeDemo();
IntersectDemo();
UnionDemo();
ToStringDemo();
Demand_DenyDemo();
PermitOnlyDemo();
UserQuota_UsageDemo();
IsSubsetOfDemo();
IsUnrestrictedDemo();
}
catch ( Exception^ e )
{
Console::WriteLine( "\r\n\nThe demo failed due to an unexpected exception\r\n{0}", e );
}
}
private:
// IsUnrestrictedDemo demonstrates the IsolatedStorageFilePermission.IsUnrestricted method.
// IsUnrestrictedDemo displays a comment indicating whether the current permission is unrestricted.
void IsUnrestrictedDemo()
{
Console::WriteLine( "IsUnrestricted demo." );
try
{
IsolatedStorageFilePermission^ isfp = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
isfp->Demand(); // The permission should be unrestricted.
Console::WriteLine( "Created and demanded the IsolatedStorageFilePermission(PermissionState.Unrestricted)permission." );
if ( true != isfp->IsUnrestricted() )
{
Console::WriteLine( "IsUnrestricted demo failed. The IsUnrestricted property value should be 'true'" );
}
isfp = gcnew IsolatedStorageFilePermission( PermissionState::None );
isfp->Demand(); // The permission should not be unrestricted.
Console::WriteLine( "Created and demanded the IsolatedStorageFilePermission(PermissionState.None)permission." );
if ( false != isfp->IsUnrestricted() )
{
Console::WriteLine( "IsUnrestricted demo failed. The IsUnrestricted property value should be 'false'" );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "Unexpected exception: {0}", e->Message );
}
}
// Copy_EqualsDemo demonstrates the Copy and Equals methods.
// Copy creates and returns an identical copy of the current permission.
// Equals compares two permissions and determines whether they are equal.
void Copy_EqualsDemo()
{
Console::WriteLine( "\nCopy_Equals demo." );
Console::WriteLine( "Creating the first IsolatedStorageFilePermission(PermissionState.Unrestricted) permission." );
IsolatedStorageFilePermission^ first = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
Console::WriteLine( "Hashcode for the first permission : {0}", first->GetHashCode() );
Console::WriteLine( "Copying the first permission." );
IsolatedStorageFilePermission^ copyOfFirst = dynamic_cast<IsolatedStorageFilePermission^>(first->Copy());
Console::WriteLine( "Hashcode for copy of the first permission: {0}", copyOfFirst->GetHashCode() );
if ( false == copyOfFirst->Equals( first ) )
{
Console::WriteLine( "The copy and the first permission should not be equal because they have "
"\n\tdifferent object references." );
}
else
{
Console::WriteLine( "Error: the copy and the first permission should not be equal because they have "
"\n\tdifferent object references." );
}
Console::WriteLine( "Does a comparison of copy of first permission to itself test equal??: {0}", (copyOfFirst->Equals( copyOfFirst ) ? (String^)"true" : "false") );
if ( false == copyOfFirst->Equals( copyOfFirst ) )
{
Console::WriteLine( "Copy_Equals demo failed because Equals returns false on two object.Equals(itself)." );
}
Console::WriteLine( "Does a comparison of first permission to itself test equal?: {0}", (first->Equals( first ) ? (String^)"true" : "false") );
if ( false == first->Equals( first ) )
{
Console::WriteLine( "Copy_Equals demo failed, because Equals returns false on two object.Equals(itself)." );
}
Console::WriteLine( "Creating a second permission with PermissionState.None." );
IsolatedStorageFilePermission^ second = gcnew IsolatedStorageFilePermission( PermissionState::None );
Console::WriteLine( "Does the first permission equal the second permission?: {0}", (first->Equals( second ) ? (String^)"true" : "false") );
if ( true == first->Equals( second ) )
{
Console::WriteLine( "Copy_Equals demo failed because Equals returns false on two object.Equals(itself)." );
}
}
// To_FromXmlDemo demonstrates ToXml and FromXml.
// ToXml creates an XML encoding of the permission and its current state.
// FromXml reconstructs a permission with a specified state from an XML encoding.
void To_FromXmlDemo()
{
Console::WriteLine( "\nTo_FromXML demo." );
IsolatedStorageFilePermission^ firstPermission = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
Console::WriteLine( "First permission: {0}", firstPermission );
SecurityElement^ se = firstPermission->ToXml();
IsolatedStorageFilePermission^ secondPermission = gcnew IsolatedStorageFilePermission( PermissionState::None );
Console::WriteLine( "Second permission: {0}", secondPermission );
secondPermission->FromXml( se );
IsolatedStorageFilePermission^ thirdPermission = dynamic_cast<IsolatedStorageFilePermission^>(secondPermission->Intersect( firstPermission ));
Console::WriteLine( "Intersection of first permission and second permission: {0}", thirdPermission );
try
{
if ( false == thirdPermission->IsUnrestricted() )
{
Console::WriteLine( "To_FromXml demo failed: The ToXml, FromXml roundtrip did not succeed." );
}
}
catch ( Exception^ e )
{
Console::WriteLine( "To_FromXml demo failed: An exception was thrown when checking the intersection" );
Console::WriteLine( "of the permissions after the XML roundtrip." );
Console::WriteLine( e );
Console::WriteLine( e );
}
}
// GetType demonstrates the GetType method.
// This method returns the type of the current instance.
void GetTypeDemo()
{
Console::WriteLine( "\nGetType demo." );
IsolatedStorageFilePermission^ isfp = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
if ( 0 != isfp->GetType()->ToString()->CompareTo( "System.Security.Permissions.IsolatedStorageFilePermission" ) )
{
Console::WriteLine( "GetType returned the wrong value: {0}", isfp->GetType() );
}
else
{
Console::WriteLine( "GetType returned: {0}", isfp->GetType() );
}
}
// IntersectDemo demonstrates the Intersect method.
// CIntersect creates and returns a permission that is the intersection of the current permission
// and the specified permission.
// Note The intersection of two PermissionState.None permissions *IS* null.
void IntersectDemo()
{
Console::WriteLine( "\nIntersect demo." );
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
IsolatedStorageFilePermission^ isfp2 = gcnew IsolatedStorageFilePermission( PermissionState::None );
IsolatedStorageFilePermission^ isfp3 = gcnew IsolatedStorageFilePermission( PermissionState::None );
IsolatedStorageFilePermission^ t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Intersect( isfp1 ));
if ( true == t->IsUnrestricted() )
{
Console::WriteLine( "The intersection of two PermissionState.Unrestricted permissions is unrestricted." );
}
else
{
Console::WriteLine( "Intersect demo failed because the intersection of two unrestricted permissions "
"\n\tshould be unrestricted." );
}
t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Intersect( isfp2 ));
if ( false == t->IsUnrestricted() )
{
Console::WriteLine( "The intersection of a PermissionState.Unrestricted and "
"\n\tPermissionState.None permission is not unrestricted." );
}
else
{
Console::WriteLine( "Intersect demo failed because the intersection of a unrestricted and "
"\n\tnone permission should be none." );
}
// The permission set that comes back from this intersection is null.
t = dynamic_cast<IsolatedStorageFilePermission^>(isfp2->Intersect( isfp3 ));
if ( nullptr == t )
{
Console::WriteLine( "The intersection of two PermissionState.None permissions is null." );
}
else
{
Console::WriteLine( "Intersect failed because the intersection of two PermissionState.None permissions "
"\n\tshould be null." );
}
}
// UnionDemo demonstrates the Union method.
// Union creates a permission that is the union of the current permission and the specified permission.
// Note: The union of two PermissionState.None permissions is *NOT* null.
void UnionDemo()
{
Console::WriteLine( "\nUnion demo." );
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
IsolatedStorageFilePermission^ isfp2 = gcnew IsolatedStorageFilePermission( PermissionState::None );
IsolatedStorageFilePermission^ isfp3 = gcnew IsolatedStorageFilePermission( PermissionState::None );
IsolatedStorageFilePermission^ t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Union( isfp1 ));
if ( true == t->IsUnrestricted() )
{
Console::WriteLine( "The union of two PermissionState.Unrestricted permissions is unrestricted." );
}
else
{
Console::WriteLine( "Union demo failed because the union of two PermissionState.Unrestricted permissions "
"\n\tshould be unrestricted" );
}
t = dynamic_cast<IsolatedStorageFilePermission^>(isfp0->Union( isfp2 ));
if ( true == t->IsUnrestricted() )
{
Console::WriteLine( "The union of a PermissionState.Unrestricted and a PermissionState.None "
"\n\tpermission is unrestricted." );
}
else
{
Console::WriteLine( "Union demo failed because the union of a PermissionState.Unrestricted and a "
"\n\tPermissionState.None permission should be unrestricted." );
}
t = dynamic_cast<IsolatedStorageFilePermission^>(isfp2->Union( isfp3 ));
if ( nullptr != t )
{
Console::WriteLine( "The union of two PermissionState.None permissions is none." );
}
else
{
Console::WriteLine( "Union failed because the union of two PermissionState.None permissions should not be null." );
Console::WriteLine( t );
}
}
// ToStringDemo demonstrates the ToString method.
// ToString creates and returns a string representation of the current permission object.
void ToStringDemo()
{
Console::WriteLine( "\nToString demo." );
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
String^ PermString = isfp0->ToString();
array<String^>^PermStringParts = {"class=","mscorlib","Version=","Culture=","PublicKeyToken=","version=","Unrestricted="};
System::Collections::IEnumerator^ myEnum = PermStringParts->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ part = safe_cast<String^>(myEnum->Current);
if ( PermString->IndexOf( part ) >= 0 )
{
Console::WriteLine( "{0} found in the string.", part );
}
else
{
Console::WriteLine( "ToString demo failed because [{0}] was not found.", part );
}
}
}
// Demand_Deny demonstrates Demand and Deny.
// Demand forces a SecurityException at run time if all callers higher in the call stack
// have not been granted the permission specified by the current instance.
// Deny prevents callers higher in the call stack from using the code that calls this method to
// access the resource specified by the current instance.
void Demand_DenyDemo()
{
Console::WriteLine( "\nDemand_Deny demo." );
Console::WriteLine( "Denying a permission with PermissionState.Unrestricted." );
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
isfp0->Deny();
try
{
Console::WriteLine( "Denying permission a second time." );
isfp0->Deny();
}
catch ( SecurityException^ )
{
Console::WriteLine( "SecurityException was thrown as expected." );
}
catch ( Exception^ e )
{
Console::WriteLine( String::Format( "Demand_Deny demo failed because an unexpected exception was thrown"
"\n\twhen making a second call to Deny()\r\n{0}", e ) );
}
Console::WriteLine( "Reverting the deny." );
IsolatedStorageFilePermission::RevertDeny();
if ( 0 != DoDemand() )
{
Console::WriteLine( "Demand_Deny demo failed because an unexpected exception was thrown during a demand." );
}
isfp0->Deny();
if ( 1 != DoDemand() )
{
Console::WriteLine( "Demand_Deny demo failed because the expected SecurityException was not thrown during a demand." );
}
IsolatedStorageFilePermission::RevertDeny();
if ( 0 != DoDemand() )
{
Console::WriteLine( "Demand_Deny demo failed because an unexpected exception was thrown during a demand." );
}
}
// PermitOnly demonstrates the PermitOnly method.
// PermitOnly prevents callers higher in the call stack from using the code that calls this method to
// access all resources except for the resource specified by the current instance.
void PermitOnlyDemo()
{
Console::WriteLine( "\nPermitOnly demo." );
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
isfp0->Demand();
isfp0->PermitOnly();
try
{
isfp0->PermitOnly();
}
catch ( SecurityException^ )
{
Console::WriteLine( "The second call to PermitOnly caused an exception as expected." );
}
catch ( Exception^ e )
{
Console::WriteLine( String::Format( "PermitOnly demo failed because an unexpected exception was thrown"
"\n\twhen making a second call to PermitOnly()\r\n{0}", e ) );
}
IsolatedStorageFilePermission::RevertPermitOnly();
if ( 0 != DemandUI() )
{
Console::WriteLine( "PermitOnly demo failed because an exception was thrown during a demand for UI permissions." );
Console::WriteLine( "We weren't expecting this." );
}
isfp0->PermitOnly();
if ( 1 != DemandUI() )
{
Console::WriteLine( "PermitOnly demo failed because a SecurityException was not thrown, "
"\n\tduring a demand for UI permissions. A SecurityException was expected." );
}
IsolatedStorageFilePermission::RevertPermitOnly();
if ( 0 != DoDemand() )
{
Console::WriteLine( "PermitOnly failed because an exception was thrown during a demand for UI permissions." );
Console::WriteLine( "We weren't expecting this." );
}
}
// UserQuota_UsageDemo demonstrates UserQuota and UsageAllowed properties.
// UserQuota gets or sets the quota on the overall size of each user's total store.
// This property is inherited from IsolatedStoragePermission.UserQuota.
// UsageAllowed gets or sets the type of isolated storage containment allowed.
// This property is inherited from IsolatedStoragePermission.UsageAllowed.
// Note You can set UsageAllowed to None, but you cannot set UserQuota to 0.
// Also note that trying to set UsageAllowed or UserQuota to values that are currently set
// will throw an exception.
[method:IsolatedStorageFilePermissionAttribute(SecurityAction::Deny,UsageAllowed=IsolatedStorageContainment::DomainIsolationByRoamingUser,UserQuota=100)]
void UserQuota_UsageDemo()
{
Console::WriteLine( "\nUserQuota_Usage demo." );
bool returnValue = false;
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::None );
Console::WriteLine( "Checking UsageAllowed for IsolatedStorageContainment.None" );
returnValue = UsageAllowed( isfp0, IsolatedStorageContainment::None, false );
if ( !returnValue )
Console::WriteLine( "IsolatedStorageContainment.None demo failed" );
Console::WriteLine( "Checking UsageAllowed for IsolatedStorageContainment.DomainIsolationByRoamingUser." );
returnValue = UsageAllowed( isfp0, IsolatedStorageContainment::DomainIsolationByRoamingUser, true );
if ( !returnValue )
Console::WriteLine( "IsolatedStorageContainment.DomainIsolationByRoamingUser failed" );
Console::WriteLine( "Checking UsageAllowed for IsolatedStorageContainment.AssemblyIsolationByUser." );
returnValue = UsageAllowed( isfp0, IsolatedStorageContainment::AssemblyIsolationByUser, true );
if ( !returnValue )
Console::WriteLine( "IsolatedStorageContainment.AssemblyIsolationByUse failed" );
Console::WriteLine( "Requesting UserQuota of 0." );
returnValue = UserQuota( isfp0, 0, true );
if ( !returnValue )
Console::WriteLine( "UserQuota 0 failed" );
Console::WriteLine( "Requesting UserQuota of 100." );
returnValue = UserQuota( isfp0, 100, true );
if ( !returnValue )
Console::WriteLine( "UserQuota 100 failed" );
Console::WriteLine( "Requesting UserQuota of -2147483648." );
returnValue = UserQuota( isfp0, Int32::MinValue, true );
if ( !returnValue )
Console::WriteLine( "UserQuota -214748364 failed" );
}
// IsSubsetOfDemo demonstrates the IsSubsetOf method.
// IsSubsetOf determines whether the current permission is a subset of the specified permission.
void IsSubsetOfDemo()
{
Console::WriteLine( "\nIsSubsetOf demo." );
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::None );
IsolatedStorageFilePermission^ isfp1 = gcnew IsolatedStorageFilePermission( PermissionState::None );
IsolatedStorageFilePermission^ isfp2 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
IsolatedStorageFilePermission^ isfp3 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
if ( isfp0->IsSubsetOf( isfp1 ) )
{
Console::WriteLine( "PermissionState.None is a subset of PermissionState.None." );
}
else
{
Console::WriteLine( "IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.None" );
}
if ( isfp0->IsSubsetOf( isfp2 ) )
{
Console::WriteLine( "PermissionState.None is a subset of PermissionState.Unrestricted." );
}
else
{
Console::WriteLine( "IsSubsetOfDemo failed: PermissionState.None should be a subset of PermissionState.Unrestricted" );
}
if ( !isfp2->IsSubsetOf( isfp0 ) )
{
Console::WriteLine( "PermissionState.Unrestricted is not a subset of PermissionState.None." );
}
else
{
Console::WriteLine( "IsSubsetOfDemo failed: PermissionState.Unrestricted should not be a subset "
"\n\tof PermissionState.None" );
}
if ( isfp2->IsSubsetOf( isfp3 ) )
{
Console::WriteLine( "PermissionState.Unrestricted is a subset of PermissionState.Unrestricted." );
}
else
{
Console::WriteLine( "IsSubsetOfDemo failed: Permissionstate.Unrestricted should be a subset of "
"\n\tPermissionState.Unrestricted" );
}
}
// **********************************************************************
// The following methods are simple helper functions.
// **********************************************************************
// **********************************************************************
// UserQuota
// Sets UserQuota to the value that was passed in.
//
bool UserQuota( IsolatedStorageFilePermission^ isfp, int user_quota, bool exception_expected )
{
bool ExceptionCaught = false;
isfp->UserQuota = user_quota;
try
{
isfp->Demand();
}
catch ( Exception^ e )
{
ExceptionCaught = true;
if ( !exception_expected )
{
Console::WriteLine( "An unexpected exception was thrown when setting UserQuota value [{0}]", user_quota );
Console::WriteLine( "Exception: \r\n{0}", e );
}
}
if ( ( !ExceptionCaught) && exception_expected )
{
Console::WriteLine( "An expected exception not thrown when setting UserQuota value [{0}]", user_quota );
}
return true;
}
// **********************************************************************
// UsageAllowed
// This method sets UsageAllowed to the value that was passed in.
//
bool UsageAllowed( IsolatedStorageFilePermission^ isfp, IsolatedStorageContainment isc, bool exception_expected )
{
bool ExceptionCaught = false;
isfp->UsageAllowed = isc;
try
{
isfp->Demand();
}
catch ( Exception^ e )
{
ExceptionCaught = true;
if ( !exception_expected )
{
Console::WriteLine( "An unexpected exception was thrown when setting IsolatedStorageContainment value [{0}]", isc );
Console::WriteLine( "Exception: \r\n{0}", e );
}
}
if ( ( !ExceptionCaught) && exception_expected )
{
Console::WriteLine( "An expected exception was not thrown when setting IsolatedStorageContainment value [{0}]", isc );
}
return true;
}
// **********************************************************************
// DoDemand()
// This method demands an IsolatedStorageFilePermission.
int DoDemand()
{
int ReturnValue = 0;
IsolatedStorageFilePermission^ isfp0 = gcnew IsolatedStorageFilePermission( PermissionState::Unrestricted );
try
{
isfp0->Demand();
}
catch ( SecurityException^ )
{
ReturnValue = 1;
}
catch ( Exception^ e )
{
Console::WriteLine( "An unexpected exception was thrown in DoDemand\r\n{0}", e );
ReturnValue = -1;
}
return ReturnValue;
}
// **********************************************************************
// DemandUI()
// This method demands a UIPermission
// Returns:
// 0 if the demand was successful.
// 1 if a SecurityException was thrown.
// -1 if an unexpected exception was thrown.
//
int DemandUI()
{
int ReturnValue = 0;
UIPermission^ MyPermission = gcnew UIPermission( PermissionState::Unrestricted );
try
{
MyPermission->Demand();
}
catch ( SecurityException^ )
{
ReturnValue = 1;
}
catch ( Exception^ e )
{
Console::WriteLine( "An unexpected exception was thrown in DemandUI\r\n{0}", e );
}
return ReturnValue;
}
};
int main()
{
Demo^ MyDemo = gcnew Demo;
MyDemo->Run();
return 0;
}