【问题标题】:C#: ShortCut and ImpersonationC#:快捷方式和模拟
【发布时间】:2016-07-18 07:25:24
【问题描述】:

我在服务器上创建快捷方式时遇到问题。它总是抛出异常:

访问被拒绝。 0x80070005

我真的不知道问题是因为快捷方式位置还是快捷方式目标。

我必须使用模拟来访问文件系统。使用IO.FileIO.Directory 我可以毫无问题地创建目录、复制和删除文件等。此外,如果我使用模拟用户打开文件存储库的远程桌面,我可以创建有任何问题的快捷方式。

但是,对于创建快捷方式,我使用的是 WHSell,它似乎在模拟方面存在一些问题。有什么想法吗?

这是我的代码:

#region Impersonation
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;

        [DllImport("advapi32.dll")]
        public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out SafeTokenHandle 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);

        private void LogonAsUser(String userName, String domain, String password)
        {

            if (!LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle))
            {
                int ret = Marshal.GetLastWin32Error();
                throw new System.ComponentModel.Win32Exception(ret);
            }
        }

        private void LogonAsUserEnd(IntPtr token)
        {
            if (!IntPtr.Equals(token, IntPtr.Zero))
            {
                CloseHandle(token);
            }

        }

        public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
        {
            private SafeTokenHandle()
                : base(true)
            {
            }

            [DllImport("kernel32.dll")]
            [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
            [SuppressUnmanagedCodeSecurity]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool CloseHandle(IntPtr handle);

            protected override bool ReleaseHandle()
            {
                return CloseHandle(handle);
            }
        }
        #endregion

private void CreateShortCut(string shorcutPath, string shortcutTarget)
        {

            IWshRuntimeLibrary.WshShell wshShell = new IWshRuntimeLibrary.WshShell();
            IWshRuntimeLibrary.IWshShortcut shortcut;
            shortcut = (IWshRuntimeLibrary.IWshShortcut)wshShell.CreateShortcut(shorcutPath);

            shortcut.TargetPath = shortcutTarget;
            shortcut.WorkingDirectory = shorcutPath;
            shortcut.Description = "Presupuestos del servicio";
            shortcut.Save();
        }

public void CreateServicioRepository(presupuestos p)
        {
            LogonAsUser(userName, domain, password);
            using (safeTokenHandle)
            {
                using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
                {
                    using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
                    {
                        try
                        {
                            if (Directory.Exists(serviceBasePath)) // serviceBasePath = "\\myserver\myfolder"
                            {
                                //Creamos el directorio para el servicio
                                string year = p.Fecha_alta.Year.ToString();
                                string serviceFolderName = p.Codigo + "_" + p.productos.Siglas;
                                if (p.IdObra.HasValue) serviceFolderName += "_" + p.obras.TituloCorto;
                                serviceFolderName = formatePath(serviceFolderName);

                                string servicePaht = Path.Combine(serviceBasePath, year, serviceFolderName);
                                if (!Directory.Exists(servicePaht)) Directory.CreateDirectory(servicePaht);

                                //Comprobamos que existe la carpeta de backend de los informes
                                string presupuestosFolder = getPresupuestoReporthPath(p); //persupuestosFolder = "\\anotherServer\anotherfolder

                                if (!Directory.Exists(presupuestosFolder)) Directory.CreateDirectory(presupuestosFolder);

                                //Creamos el acceso directo a la carpeta de informes generados en presupuesto
                                if (!string.IsNullOrWhiteSpace(presupuestosFolder))
                                {
                                    string shortCutName = "001_" + p.Codigo + "_Presupuesto.lnk";
                                    string shortCutPath = Path.Combine(servicePaht, shortCutName);

                                    if (!File.Exists(shortCutPath)) CreateShortCut(shortCutPath, presupuestosFolder);
                                }

                                //Comprobamos que existe la carpeta de backend de los informes
                                string pavoFolder = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["pavoPath"], p.Codigo);

                                if (!Directory.Exists(pavoFolder)) Directory.CreateDirectory(pavoFolder);

                                //Creamos el acceso directo a la carpeta de PAVO
                                if (p.productos.IdModulo == (int)EnumsHelper.Modulos.Obras)
                                {
                                    string pavoShortCutName = "002_" + p.Codigo + "_PVOD.lnk";
                                    string pavoShortCutPath = Path.Combine(servicePaht, pavoShortCutName);

                                    if (!File.Exists(pavoShortCutPath)) CreateShortCut(pavoShortCutPath, pavoFolder);
                                }

                                //Copiamos las plantilla de la estructura de carpetas del servicio
                                if (Directory.Exists(directoryTemplate) && p.IdEstado == (int)EnumsHelper.EstadoPresupuesto.Aprobado)
                                {
                                    foreach (string dirName in Directory.GetDirectories(directoryTemplate))
                                    {
                                        if (dirName.Trim().ToLower().Contains(p.productos.Nombre.Trim().ToLower()))
                                        {
                                            string originalPath = Path.Combine(directoryTemplate, dirName);
                                            CopyDirectory(originalPath, servicePaht, p.Codigo);
                                            break;
                                        }
                                    }
                                }

                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Error(ex);
                            throw ex;
                        }
                    }
                }
            }                       
        }

【问题讨论】:

    标签: c# shortcut impersonation


    【解决方案1】:

    作为一种解决方法,您可以禁用模拟,然后使用 shell 在临时目录中创建快捷方式文件。然后,您可以将此文件作为字节数组传递给模拟代码,在那里可以将其保存到正确的路径。

    public void CreateShortCut(string shorcutPath, string shortcutTarget)
    {
        byte[] bytes = null;
    
        // Disable impersonation
        using (System.Security.Principal.WindowsImpersonationContext ctx = System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero))
        {
            // Get a temp file name (the shell commands won't work without .lnk extension)
            var path = Path.GetTempPath();
            string temp = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".lnk");
            try
            {
                WshShell shell = new WshShell();
                IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(temp);
                shortcut.TargetPath = shortcutTarget;
                shortcut.Save();
                bytes = System.IO.File.ReadAllBytes(temp);
            }
            finally
            {
                if (System.IO.File.Exists(temp)) System.IO.File.Delete(temp);
            }
        }
    
        // Impersonation resumed
        System.IO.File.WriteAllBytes(shorcutPath, bytes);
    }
    

    【讨论】:

    • 这不适用于我的情况。在您的代码中,当您执行 shortcut.Save() 时,它要求使用的用户有权访问目标位置。我无法禁用模拟,因为 IIS_IUSER(默认使用的用户)无法访问目标目录,并且我无法授予对它的访问权限。所以这一行会抛出异常
    • 只要存在“默认”用户(禁用模拟时假定的身份)具有写入权限的目录,这应该可以工作。在我的 sn-p 中,这个“暂存”目录恰好是 Path.GetTempPath() 提供的临时目录,因为它适用于我的 IIS 应用程序池的身份。
    猜你喜欢
    • 2012-05-20
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    相关资源
    最近更新 更多