Environment.SpecialFolder和Impersonation

时间:2017-03-14 10:16:59

标签: c# impersonation special-folders

我使用Visual Studio 2013,Windows 8.1。

我尝试从模拟用户获取Environment.SpecialFolder值,但我总是空字符串值

有什么建议吗?。

我的测试代码:

    [TestMethod]
    public void Environment_Folders_is_ok()
    {
        Console.WriteLine("CURRENT USER " + Environment.UserName);
        GlobalVariables.MyDocumentsPath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        Console.WriteLine(GlobalVariables.MyDocumentsPath);

        var p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        Console.WriteLine(p1);
        p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        Console.WriteLine(p1);
        p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        Console.WriteLine(p1);
        p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
        Console.WriteLine(p1);

        var user = new Utils().DecryptValue(ConfigurationManager.AppSettings["User"]);
        var password = new Utils().DecryptValue(ConfigurationManager.AppSettings["Password"]);
        var domain = new Utils().DecryptValue(ConfigurationManager.AppSettings["Domain"]);

        GlobalVariables.Impersonate = true;

        using (new Impersonator(user, domain, password))
        {
            Console.WriteLine("IMPERSONATE with " +  user);
            p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            Console.WriteLine(p1);
            p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            Console.WriteLine(p1);
            p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            Console.WriteLine(p1);
            p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            Console.WriteLine(p1);

       }

    }

输出:

  

CURRENT USER MyUser

     

C:\用户\ MYUSER \文件
  C:\用户\ MYUSER
  C:\用户\ MYUSER \桌面
  C:\ Users \ MyUser \ Desktop

     

使用user2进行IMPERSONATE

更新

同样,我使用FolderBrowserDialog和方法ShowDialog,我收到错误

  

无法检索根文件夹

当我使用模拟用户

            try
            {
                Console.WriteLine("FolderBrowserDialog. IMPERSONATE WITH " + user);
                using (var fbd = new FolderBrowserDialog())
                {
                    Console.WriteLine(fbd.RootFolder);

                    p1 = System.Environment.GetFolderPath(fbd.RootFolder);
                    Console.WriteLine(p1);

                    DialogResult result = fbd.ShowDialog();
                }
            }
            catch (System.InvalidOperationException ex)
            {
                Assert.AreEqual("Unable to retrieve the root folder.", ex.Message, "No se esperaba " + ex.Message);
            }

输出:

  

的FolderBrowserDialog。用户2的IMPERSONATE   桌面

1 个答案:

答案 0 :(得分:0)

我使用LOGON32_LOGON_NEW_CREDENTIALSLogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50)来模仿

https://platinumdogs.me/2008/10/30/net-c-impersonation-with-network-credentials/

我获得的路径与用户MyUser相同。

我的代码:

   using (new Impersonator(user, domain, password
                   , LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50
                ))
  {
     Console.WriteLine("IMPERSONATE LOGON32_LOGON_NEW_CREDENTIALS with " + user);
         p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
         Console.WriteLine(p1);
         p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
         Console.WriteLine(p1);
         p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
         Console.WriteLine(p1);
         p1 = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
         Console.WriteLine(p1);
     }

<强>输出:

  

使用user2

表示LOGON32_LOGON_NEW_CREDENTIALS      

C:\用户\ MYUSER \文件
  C:\用户\ MYUSER
  C:\用户\ MYUSER \桌面
  C:\ Users \ MyUser \ Desktop