// The Trusted Code.
using System;
namespace TrustedCode
{
public class PerformPrivilgedOperations
{
public static void PrivilgedOperations()
{
// Some Call to Privileged Code.
}
}
}
_______________________________________________________________________________________________
// The Façade Code (with APTCA applied)
using System;
using System.Security;
using System.Security.Permissions;
using System.Reflection;
using TrustedCodeWithImproperAPTCA;
// Applying APTCA to assembly
[assembly:AllowPartiallyTrustedCallers]
namespace FacadeWithAPTCACallingTrustedCode
{
public class FacadeCallingTrustedCode
{
public static void CallingTrustedCode()
{
// This security check fails if the caller
// does not have full trust.
NamedPermissionSet pset= new NamedPermissionSet("FullTrust");
// This try-catch block shows the caller's permissions.
// Correct code would either not catch the exception,
// or would rethrow it.
try
{
pset.Demand();
}
catch (SecurityException e)
{
Console.WriteLine("Demand for full trust:{0}", e.Message);
}
// Calls PrivilgedOperations()
PerformPrivilgedOperations.PrivilgedOperations();
}
}
}
// The Un-trusted / Partial Code
using System;
using FacadeWithAPTCACallingTrustedCode;
// If this test is run from the local computer, it gets full trust by default.
// Remove full trust.
[assembly:System.Security.Permissions.PermissionSetAttribute(
System.Security.Permissions.SecurityAction.RequestRefuse, Name="FullTrust")]
namespace TestSecLibrary
{
class TestApctaMethodRule
{
public static void Main()
{
FacadeCallingTrustedCode.CallingTrustedCode();
}
}
}