我有这个类,我在AppDomain中创建这个类的实例除了SecurityPermissionFlag.Execute之外没有任何权限。
class IsolationEntryPoint : MarshalByRefObject
{
// main is the original AppDomain with all the permissions
public void Enter(AppDomain main)
{
// these work correctly
Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Host: " + main.FriendlyName);
// the exception is thrown here
main.DoCallBack(this.MyCallBack);
}
public void MyCallBack()
{
Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
}
}奇怪的是,我看到SecurityException在DoCallback行中说:
请求'System.Security.Permissions.ReflectionPermission,mscorlib、Version=4.0.0.0、Culture=neutral、PublicKeyToken=b77a5c561934e089‘类型的权限失败。
MSDNsays 这关于AppDomain.DoCallBack的权限要求:
当通过诸如ReflectionPermission等机制调用后期绑定时,就会出现Type.InvokeMember。
调用没有使用像 Type.InvokeMember这样的东西,为什么我会得到异常呢?
编辑
为了清晰起见,下面是使用隔离对象创建AppDomain的代码:
[STAThread]
static void Main(string[] args)
{
var setup = new AppDomainSetup();
setup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
var evidence = new Evidence();
var permissions = new PermissionSet(PermissionState.None);
permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
var domain = AppDomain.CreateDomain(
"isolationDomain",
evidence,
setup,
permissions);
var handle = Activator.CreateInstanceFrom(
domain, typeof(IsolationEntryPoint).Assembly.ManifestModule.FullyQualifiedName,
typeof(IsolationEntryPoint).FullName);
var instance = (IsolationEntryPoint)handle.Unwrap();
instance.Enter(AppDomain.CurrentDomain);
}这两段代码是我的完整应用程序,没有其他任何东西(所以异常应该很容易再现)。
谢谢你的帮忙
发布于 2012-01-25 12:24:20
解决方案实际上非常简单:您忽略了向class IsolationEntryPoint添加公共访问修饰符,即在更改类签名之后,您的示例运行得很好:
public class IsolationEntryPoint : MarshalByRefObject
{
// [...]
}发布于 2012-01-21 19:29:34
我试过下面的方法,看起来很管用。
class Program
{
static void Main(string[] args)
{
SecurityPermission t = new SecurityPermission(SecurityPermissionFlag.Execution);
t.Demand();
IsolationEntryPoint x = new IsolationEntryPoint();
x.Enter(AppDomain.CurrentDomain);
}
}
class IsolationEntryPoint : MarshalByRefObject
{
// main is the original AppDomain with all the permissions
public void Enter(AppDomain main)
{
// these work correctly
Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
Console.WriteLine("Host: " + main.FriendlyName);
// the exception is thrown here
main.DoCallBack(this.MyCallBack);
}
public void MyCallBack()
{
Console.WriteLine("Currently in: " + AppDomain.CurrentDomain.FriendlyName);
}
}https://stackoverflow.com/questions/8954881
复制相似问题