I want to allow calling the method only from the particular methods. Take a look at the code below.
private static void TargetMethod()
{
}
private static void ForbiddenMethod()
{
TargetMethod();
}
private static void AllowedMethod()
{
TargetMethod();
}
I need only AllowedMethod could call TargetMethod. How to do it using classes from System.Security.Permissions?
Updated: Thanks for your answers, but I don’t want to debate about design of my application. I just want to know is it possible to do it using .net security or not?
CAS cannot do this if the code is running in full trust.
If the code is executing in Full Trust (i.e. a normal, local application, not a silverlight application or something run from the network), then all .NET CAS checks are completely bypassed; any security attribute is simply ignored.
CAS simply walks the stack to determine permissions, though, and you can do that too, as Darin pointed out earlier by just checking the
StackTrace.