创建具有完全信任权限(包括网络权限)的托管CLR AppDomain

Ric*_*ahl 7 c# c++ clr-hosting security-policy

我需要在非托管进程中托管.NET运行时.我有通过COM加载运行时的代码,我可以将程序集加载到AppDomain并执行代码就好了.

但是,我遇到了托管在网络共享上的应用程序的问题,并且必须更改应用程序策略才能让它们执行而不是一个选项.所以我想要做的是将运行时的主AppDomain的权限级别设置为不受限制.

有人可以举例说明如何设置AppDomain策略级别吗?我无法弄清楚如何从非托管代码实例化所需的类来创建PolicyLevel和相关对象并设置策略.基本上我不知道我需要从我使用的C++代码中使用什么包含/命名空间引用.

这是我此时的代码:

/// Starts up the CLR and creates a Default AppDomain
DWORD WINAPI ClrLoad(char *ErrorMessage, DWORD *dwErrorSize)
{
    if (spDefAppDomain)
        return 1;


    //Retrieve a pointer to the ICorRuntimeHost interface
    HRESULT hr = CorBindToRuntimeEx(
                    ClrVersion, //Retrieve latest version by default
                    L"wks", //Request a WorkStation build of the CLR
                    STARTUP_LOADER_OPTIMIZATION_MULTI_DOMAIN | STARTUP_CONCURRENT_GC, 
                    CLSID_CorRuntimeHost,
                    IID_ICorRuntimeHost,
                    (void**)&spRuntimeHost
                    );

    if (FAILED(hr)) 
    {
        *dwErrorSize = SetError(hr,ErrorMessage);   
        return hr;
    }

    //Start the CLR
    hr = spRuntimeHost->Start();

    if (FAILED(hr))
        return hr;

    CComPtr<IUnknown> pUnk;

    //Retrieve the IUnknown default AppDomain
    //hr = spRuntimeHost->GetDefaultDomain(&pUnk);
    //if (FAILED(hr)) 
    //  return hr;


    WCHAR domainId[50];
    swprintf(domainId,L"%s_%i",L"wwDotNetBridge",GetTickCount());
    hr = spRuntimeHost->CreateDomain(domainId,NULL,&pUnk);  

    hr = pUnk->QueryInterface(&spDefAppDomain.p);
    if (FAILED(hr)) 
        return hr;

      // // Create a new AppDomain PolicyLevel.
   //PolicyLevel polLevel = PolicyLevel:: CreateAppDomainLevel();

   //// Create a new, empty permission set.
   // PermissionSet permSet = gcnew PermissionSet( PermissionState::Unrestricted);

   //// Add permission to execute code to the permission set.
   //permSet->AddPermission( gcnew SecurityPermission( SecurityPermissionFlag::Execution ) );

   ////// Give the policy level's root code group a new policy statement based
   ////// on the new permission set.
   ////polLevel->RootCodeGroup->PolicyStatement = gcnew PolicyStatement( permSet );

   //// Give the new policy level to the application domain.
   //spDefAppdomain->SetAppDomainPolicy( polLevel );



    return 1;
}
Run Code Online (Sandbox Code Playgroud)

我选择了一些示例代码(注释)似乎做了我需要它,但我无法弄清楚我需要什么样的lib/include引用才能使PermissionSet和PolicyLevel的类型引用工作.

任何想法非常感谢...

Jer*_*all 2

我认为你需要使用“不平凡”的方法来创建一个AppDomain才能获得任何好处:

  • CreateDomainSetup(IUnknown** pAppDomainSetup),这会让你返回一个IAppDomainSetup实例。
  • 适当填写(我认为所有政策内容都在那里)
  • 使用CreateDomainEx,传入初始化的设置实例作为第二个参数
  • 利润?

参考: