1

我创建了一个 MMC 管理单元,它在新的 appdomain 中启动代码,并且部分代码会检查注册表项。如果我在 snap in 过程中检查密钥,它可以工作,但新 appdomain 中的代码会引发安全异常。如果我从控制台或 Windows 应用程序将代码加载到新的 appdomain 中,它工作正常。

这是代码:

public class SimpleMMCSnapIn : SnapIn  
{  
    public SimpleMMCSnapIn()
    {
        RegistryKey archerKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft", true); //this call works

        Evidence baseEv = AppDomain.CurrentDomain.Evidence;
        Evidence newEv = new Evidence(baseEv);

        AppDomainSetup setup = new AppDomainSetup { ApplicationBase = "<pathtobin>" };

        AppDomain domain = AppDomain.CreateDomain("MigratorDomain", newEv, setup);
        domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

        IWork migrator = (IWork)domain.CreateInstanceAndUnwrap("CheckRegistry", "CheckRegistry.CheckRegistry");

        migrator.Work();
    }
}

[Serializable]  
public class CheckRegistry : MarshalByRefObject, IWork  
{  
    public void Work()  
    {  
        RegistryKey archerKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft", true); //this call throws a security exception
    }  
}

请注意,如果我从控制台或 Windows 应用程序将代码加载到新的 appdomain 中,它可以正常工作。我认为这更像是一个 MMC 管理单元问题,而不是 UAC 问题。

任何见解将不胜感激......

谢谢,

布拉德

4

1 回答 1

0

如果您更改 Work() 方法来执行此操作,您会看到什么?

WindowsPrincipal user = (WindowsPrincipal)Thread.CurrentPrincipal;
if ( user.IsInRole(WindowsBuiltInRole.Administrator) )
{
    MessageBox.Show(string.Format("{0} is an Administrator", user.Identity.Name));
}
else
{
    MessageBox.Show(string.Format("{0} is NOT an Administrator", user.Identity.Name));
}
于 2010-01-28T19:00:29.353 回答