永发信息网

知道电脑ip用户名和密码C#怎么获取里面文件路径

答案:2  悬赏:20  手机版
解决时间 2021-02-03 13:17
  • 提问者网友:心如荒岛囚我终老
  • 2021-02-03 01:35
知道电脑ip用户名和密码C#怎么获取里面文件路径
最佳答案
  • 五星知识达人网友:鸠书
  • 2021-02-03 01:48
代码见下:
    ///   
    /// 1.局域网内跨主机远程文件访问类,实现模拟登录,获取凭证后即可同本机路径一样访问  
    /// 
  
    ///   
    /// invoke demo,详见方法中的TestFunc:  
    /// RemoteFileHelper rm = new RemoteFileHelper();  
    ///  if (rm.impersonateValidUser("administrator", "\\192.168.193.50", "******"))  
    ///  {System.IO.File.Copy(@"\\192.168.193.50\123\platV0.1.zip", @"E:\\f1\\platV0.1.zip", true);   
    ///  System.IO.File.Copy(@"E:\\f1\\platV0.1.zip",@"\\192.168.193.50\123\platV0.2.zip", true);}  
    /// 
  
    public class RemoteFileHelper  
    {  
        // 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 WindowsImpersonationContext impersonationContext;  

        public 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 void undoImpersonation()  
        {  
            impersonationContext.Undo();  
        }  

        public void Test()  
        {  
            bool isImpersonated = false;  
            try  
            {  
                if (impersonateValidUser("UserName", "Domain", "Password"))  
                {  
                    isImpersonated = true;  
                    File.Copy(@"\\192.168.1.48\generals\now.htm", "c:\\now.htm", true);  
                }  
            }  
            finally  
            {  
                if (isImpersonated)  
                    undoImpersonation();  
            }  
        }  
    }引用:CSDN- 《C#访问远程主机资源的方法汇总和对比》@RocChenKing
全部回答
  • 1楼网友:孤独的牧羊人
  • 2021-02-03 02:40
1. 在asp.net中专用属性: 获取服务器电脑名:page.server.manchinename 获取用户信息:page.user 获取客户端电脑名:page.request.userhostname 获取客户端电脑ip:page.request.userhostaddress 2. 在网络编程中的通用方法: 获取当前电脑名:static system.net.dns.gethostname() 根据电脑名取出全部ip地址:static system.net.dns.resolve(电脑名).addresslist 也可根据ip地址取出电脑名:static system.net.dns.resolve(ip地址).hostname 3. 系统环境类的通用属性: 当前电脑名:static system.environment.machinename 当前电脑所属网域:static system.environment.userdomainname 当前电脑用户:static system.environment.username
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯