zoukankan      html  css  js  c++  java
  • 将文件写到另一台服务器

     //通过模拟用户获得权限登录
        public class ConnectHelper
        {
            // logon types
            const int LOGON32_LOGON_INTERACTIVE = 2;
            const int LOGON32_LOGON_NETWORK = 3;
            const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
            // logon providers
            const int LOGON32_PROVIDER_DEFAULT = 0;
            const int LOGON32_PROVIDER_WINNT50 = 3;
            const int LOGON32_PROVIDER_WINNT40 = 2;
            const int LOGON32_PROVIDER_WINNT35 = 1;

            [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern int LogonUser(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);

            private static WindowsImpersonationContext impersonationContext;

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

                if (RevertToSelf())
                {
                    // 这里使用LOGON32_LOGON_NEW_CREDENTIALS来访问远程资源。
                    // 如果要(通过模拟用户获得权限)实现服务器程序,访问本地授权数据库可
                    // 以用LOGON32_LOGON_INTERACTIVE
                    if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS,
                        LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                    {
                        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                        {
                            tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                            impersonationContext = tempWindowsIdentity.Impersonate();
                            if (impersonationContext != null)
                            {
                                System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                                IPrincipal pr = System.Threading.Thread.CurrentPrincipal;
                                IIdentity id = pr.Identity;
                                CloseHandle(token);
                                CloseHandle(tokenDuplicate);
                                return true;
                            }
                        }
                    }
                }

                if (token != IntPtr.Zero)
                    CloseHandle(token);

                if (tokenDuplicate != IntPtr.Zero)
                    CloseHandle(tokenDuplicate);

                return false;
            }

            public static void undoImpersonation()
            {
                impersonationContext.Undo();
            }

         
        }

        /// <summary>
            /// 将文件写到另一台服务器
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="s"></param>
            private static void WriteFile(string fileName, string s)
            {
                try
                {
                    if (ConnectHelper.impersonateValidUser(username, endpoint, password))
                    {
                        log("访问远程服务器成功"+DateTime.Now.ToString());
                        string objPath = ConfigurationManager.AppSettings["xmlFilePath"];
                        FileStream fsw = new FileStream(objPath + fileName.ToString().Trim() + ".xml", FileMode.Create, FileAccess.Write);
                        StreamWriter sw = new StreamWriter(fsw, Encoding.UTF8);
                        sw.BaseStream.Seek(0, SeekOrigin.End);
                        sw.Write(s);
                        sw.Flush();
                        sw.Close();
                        fsw.Close();
                    }
                }
                catch (Exception ex)
                {
                    log("访问远程服务器失败" + DateTime.Now.ToString() + ex.ToString());
                    throw;
                }
                finally
                {
                    ConnectHelper.undoImpersonation();
                }

            }

    转自http://www.cnblogs.com/h2appy/articles/1204277.html

  • 相关阅读:
    C#中的扩展方法
    对象的序列化存入数据库,与反序列化
    PowerDesigner15:EAM(Enterprise Architecture Model)企业架构模组
    WPF优化:加速启动时间
    LINQ优化:将GroupBy换做Distinct
    WPF:CheckBox竖向的滑块效果
    微軟提議﹕c#編程四個注意
    Remoting:于.net框架下的序列化機制
    c#編寫聖誕樹算法﹐及相關問題。
    72
  • 原文地址:https://www.cnblogs.com/wenbing/p/3604617.html
Copyright © 2011-2022 走看看