冒充用户不工作

时间:2013-10-24 08:55:51

标签: c# asp.net .net c#-4.0 asp.net-4.0

我正在尝试将文件复制到局域网中的另一台PC上。到目前为止,这是我的代码:

try
        {
            IntPtr token = IntPtr.Zero;

            bool result = Impersonation.LogonUser("timtim", "testDom",
                                        "kitkit",
                                        Impersonation.LogonSessionType.Network,
                                        Impersonation.LogonProvider.Default,
                                        out token);


            if (result)
            {
                System.Security.Principal.WindowsIdentity id = new System.Security.Principal.WindowsIdentity(token);

                // Begin impersonation
                impersonatedUser = id.Impersonate();
                // Log the new identity
                Response.Write(String.Format(
                               "</p>Identity after impersonation: {0}<br>",
                               System.Security.Principal.WindowsIdentity.GetCurrent().Name));

                System.IO.File.Copy(@"D:\sites\test\XXX.pdf", @"\\testDom\ins\00000000000.pdf");

                // Resource access here uses the impersonated identity
            }
        }
        finally
        {
            impersonatedUser.Undo();
        }

这是我的Impersonation类:

 public class Impersonation
    {

        [DllImport("advapi32.dll", SetLastError = true)]
        public static extern bool LogonUser(
          string principal,
          string authority,
          string password,
          LogonSessionType logonType,
          LogonProvider logonProvider,
          out IntPtr token);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool CloseHandle(IntPtr handle);

        public enum LogonSessionType : uint
        {
            Interactive = 2,
            Network,
            Batch,
            Service,
            NetworkCleartext = 8,
            NewCredentials
        }

        public enum LogonProvider : uint
        {
            Default = 0, // default for platform (use this!)
            WinNT35,     // sends smoke signals to authority
            WinNT40,     // uses NTLM
            WinNT50      // negotiates Kerb or NTLM
        }

    }

程序抛出错误:

System.IO.File.Copy(@"D:\sites\test\XXX.pdf", @"\\testDom\ins\00000000000.pdf");
  

“访问被拒绝”

我错过了什么?

0 个答案:

没有答案