我在整个C#应用程序中使用SimpleImpersonation来简化Win32 LogonUser功能的使用。到目前为止,这在所有情况下都可以正常工作,除了我需要在具有不同凭据的远程计算机上创建COM对象实例的特殊情况。
有没有办法用针对具有备用凭据的远程计算机的Activator.CreateInstance()
打包?无论我通过LogonType
的哪个值,我都会拒绝访问(0x80070005)。
using (Impersonation.LogonUser(domain, username, password, LogonType.Interactive))
{
try
{
Type t = Type.GetTypeFromProgID("Microsoft.Update.Session", hostname, true);
var comobj = Activator.CreateInstance(t); // <== throws UnauthorizedAccessException
UpdateSession session = (UpdateSession) comobj;
IUpdateSearcher updateSearcher = session.CreateUpdateSearcher();
var count = updateSearcher.GetTotalHistoryCount();
...
为了进行比较,如果我以不同的用户身份启动提示,则可以从Powershell中正常工作:
PS C:\WINDOWS\system32> $updatesession = [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.Session", $hostname))