1
是否有可能獲得ConfigurationManager.OpenExeConfiguration以使用當前模擬的用戶(當模擬正在進行時,類似於WindowsImpersonationContext的代碼示例) - 以下是一個小提取?帶有模擬的用戶設置/配置
using (safeTokenHandle)
{
Console.WriteLine("Did LogonUser Succeed? " + (returnValue ? "Yes" : "No"));
Console.WriteLine("Value of Windows NT token: " + safeTokenHandle);
// Check the identity.
Console.WriteLine("Before impersonation: "
+ WindowsIdentity.GetCurrent().Name);
Configuration config;
//config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
//Console.WriteLine("Local user config path: {0}", config.FilePath);
// Use the token handle returned by LogonUser.
using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
{
using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
{
// Check the identity.
Console.WriteLine("After impersonation: " + WindowsIdentity.GetCurrent().Name);
// This line throws exception
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
Console.WriteLine("Local user config path: {0}", config.FilePath);
}
}
// Releasing the context object stops the impersonation
// Check the identity.
Console.WriteLine("After closing the context: " + WindowsIdentity.GetCurrent().Name);
}
如果我只是添加了模擬的範圍內通話時,我得到拋出的異常:
Exception occurred. An error occurred loading a configuration file: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
如果我還冒充塊之前調用OpenExeConfiguration,那麼第二個呼叫(塊內)不會失敗,但會返回原始用戶的路徑。