当前位置: 首页>>代码示例>>C#>>正文


C# WindowsIdentity.Undo方法代码示例

本文整理汇总了C#中System.Security.Principal.WindowsIdentity.Undo方法的典型用法代码示例。如果您正苦于以下问题:C# WindowsIdentity.Undo方法的具体用法?C# WindowsIdentity.Undo怎么用?C# WindowsIdentity.Undo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Principal.WindowsIdentity的用法示例。


在下文中一共展示了WindowsIdentity.Undo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DownloadCifsFileToLocalFile

        /// <summary>
        /// Copy file on network share to local volume.
        /// </summary>
        /// <remarks>
        /// Access to the network share is acheived by logging into the domain corresponding to the user credentials provided.
        /// Windows impersonation does not suffice, because impersonation is limited to domains with an established trust relationship.
        /// We have had to import Win32 API calls to allow login.  There are a number of examples online.  We follow the
        /// one at http://stackoverflow.com/a/2541569/939250 </remarks>
        /// <param name="filePathRelativeToShare"></param>
        /// <param name="cifsShareDetails"></param>
        /// <param name="destFile"></param>
        public static void DownloadCifsFileToLocalFile(string filePathRelativeToShare, NFSTO cifsShareDetails, string destFile)
        {
            try
            {
                IntPtr token = IntPtr.Zero;

                LogonUser(cifsShareDetails.User, cifsShareDetails.Domain, cifsShareDetails.Password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token);
                using (WindowsImpersonationContext remoteIdentity = new WindowsIdentity(token).Impersonate())
                {
                    String dest = "";
                    if (filePathRelativeToShare.EndsWith(".iso") || filePathRelativeToShare.EndsWith(".vhd") || filePathRelativeToShare.EndsWith(".vhdx"))
                    {
                        dest = Path.Combine(cifsShareDetails.UncPath, filePathRelativeToShare);
                        dest = Utils.NormalizePath(dest);
                    }
                    // if the filePathRelativeToShare string don't have filename and only a dir point then find the vhd files in that folder and use
                    // In the clean setup, first copy command wont be having the filename it contains onlyu dir path.
                    // we need to scan the folder point and then copy the file to destination.
                    else if (!filePathRelativeToShare.EndsWith(".vhd") || !filePathRelativeToShare.EndsWith(".vhdx"))
                    {
                        // scan the folder and get the vhd filename.
                        String uncPath = Path.Combine(cifsShareDetails.UncPath, Path.Combine(filePathRelativeToShare.Split('/')));
                        //uncPath = uncPath.Replace("/", "\\");
                        DirectoryInfo dir = new DirectoryInfo(uncPath);
                        FileInfo[] vhdFiles = dir.GetFiles("*.vhd*");
                        if (vhdFiles.Length > 0)
                        {
                            FileInfo file = vhdFiles[0];
                            dest = file.FullName;
                        }
                    }

                    s_logger.Info(CloudStackTypes.CopyCommand + ": copy " + Path.Combine(cifsShareDetails.UncPath, filePathRelativeToShare) + " to " + destFile);
                    File.Copy(dest, destFile, true);
                    remoteIdentity.Undo();
                }
            }
            catch (UnauthorizedAccessException ex)
            {
                string errMsg = "Invalid user or password for the share " + cifsShareDetails.UncPath;
                s_logger.Error(errMsg);

                throw new ArgumentException(errMsg, ex);
            }
        }
开发者ID:ktenzer,项目名称:cloudstack,代码行数:56,代码来源:Utils.cs


注:本文中的System.Security.Principal.WindowsIdentity.Undo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。