Azure虚拟机试图模拟和访问文件

时间:2019-02-20 22:10:06

标签: azure c#-4.0 impersonation ntfs

这是我的界面。我在Azure虚拟机中有2个用户,第一个是“管理员”,另一个是“用户”。我的“用户”无权访问我的文件C:/direc/test.txt,但我的“管理员”拥有所有权限。

因此,我试图以管理员身份模拟,因此我可以以“用户”身份访问C:/direc/test.txt。

但是,它给了我这个疯狂的错误:
But, it give me this crazy error

  

表达式:[mscorlib递归资源查找错误]
  说明:mscorlib中资源查找期间的无限递归。
  这可能是mscorlib中的错误,也可能是某些可扩展性中的错误,例如程序集解析事件或CultureInfo名称。资源名称:UnknownError_Num

这是我班上的假人

public class CodeImpersonate : IDisposable
    {
        /// <summary>
        /// This logon type is intended for users who will be interactively using the computer, such as a user being logged on by a terminal server, 
        /// remote shell, or similar process. This logon type has the additional expense of caching logon information for disconnected operations; therefore,
        /// it is inappropriate for some client/server applications, such as a mail server.
        /// </summary>
        public const int LOGON32_LOGON_INTERACTIVE = 9;

        /// <summary>
        /// Use the standard logon provider for the system. The default security provider is negotiate, 
        /// unless you pass NULL for the domain name and the user name is not in UPN format. In this case, the default provider is NTLM.
        /// Windows 2000: The default security provider is NTLM.
        /// </summary>
        public const int LOGON32_PROVIDER_DEFAULT = 0;

        WindowsImpersonationContext impersonationContext;

        [DllImport("advapi32.dll")]
        public static extern int LogonUserA(String lpszUserName,
            String lpszDomain,
            String lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            ref IntPtr phToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();

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

        public bool ImpersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity tempWindowsIdentity;
            IntPtr token = IntPtr.Zero;
            IntPtr tokenDuplicate = IntPtr.Zero;

            // Console.WriteLine("Before impersonation: "
            //+ WindowsIdentity.GetCurrent().Name);
            try
            {
                if (RevertToSelf())
                {
                    if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
                            ref token) != 0)
                    {
                        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                        {
                            tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);


                            impersonationContext = tempWindowsIdentity.Impersonate();
                            if (impersonationContext != null)
                            {
                                Console.WriteLine("After impersonation: "+
                                WindowsIdentity.GetCurrent().Name);
                                CloseHandle(token);
                                CloseHandle(tokenDuplicate);
                                return true;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occurred. " + e.Message);
            }


            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
        }

        public void UndoImpersonation()
        {
            if (impersonationContext != null)
                impersonationContext.Undo();
        }

        public void printContext()
        {
            Console.WriteLine(impersonationContext.ToString());
            Console.ReadLine();
        }
        public void Dispose()
        {
            impersonationContext.Dispose();
        }
    }

这是我的主代码:

string currentPath = @"C:\direc";
            string currentFileName = "test";
            string currentFileExt = ".txt";

            try
            {
                FileOnDesktop = OpenFileFromCMS(currentPath, currentFileName, currentFileExt);
            }catch(Exception e)
            {
                Console.WriteLine(e);
            }



 private static bool OpenFileFromCMS(string currentPath, string currentFileName, string currentFileExt)
            {


                Byte[] arrayByte = null;
                CodeImpersonate impersonationDemo = new CodeImpersonate();

                    bool a = impersonationDemo.ImpersonateValidUser(user, domain, pwd);
                using (new Impersonation("domain", "admin", "123456"))
                {
                    string ActiveUser = WindowsIdentity.GetCurrent().Name;
                        string[] fileArray = Directory.GetFiles(currentPath);
                }

0 个答案:

没有答案