沐水dev 2019-07-03 14:51 采纳率: 0%
浏览 711

C#客户端使用LogonUser访问服务器上的共享文件,提示用户名或密码错误(客户端与服务器在同一域中)

 private static bool IfUpdate()
        {
            bool bRtn = true;
            try
            {
                string AppDataPath = System.Windows.Forms.Application.StartupPath;//程序根目录
                //从服务器copy文件
                //System.IO.File.Copy(@"\\dev-server\D\csv.csv", @"D:\TEST.txt", true);

                IntPtr admin_token = default(IntPtr);
                WindowsIdentity wid_admin = null;
                WindowsImpersonationContext wic = null;

                string struser = global::InventoryApplication.Properties.Settings.Default.User;//用户名
                string strpwd = global::InventoryApplication.Properties.Settings.Default.Pwd;//密码
                string strIP = global::InventoryApplication.Properties.Settings.Default.URL; //服务器IP
                string strurl = global::InventoryApplication.Properties.Settings.Default.Route;//共享目录路径

                //在程序中模拟域帐户登录  
                if (LogonUser(struser, strIP, strpwd, 9, 0, ref admin_token) != 0)
                {
                    using (wid_admin = new WindowsIdentity(admin_token))
                    {
                        using (wic = wid_admin.Impersonate())
                        {
                            //假定要操作的文件路径是  
                            FileInfo file = new FileInfo(@"\\" + strIP + "\\" + strurl + "\\" + COMMON.ClsCommon.strUpdatetxt + "");

                            string strFILE = AppDataPath + COMMON.ClsCommon.strUpdatetxt + "";
                            if (File.Exists(strFILE))
                            {
                                File.Delete(strFILE);
                            }

                            //copy版本号
                            file.CopyTo(AppDataPath + COMMON.ClsCommon.strUpdatetxt + "");

                        }
                    }
                }
                else
                {
                    COMMON.ClsCommon.ErrorMassage("连接失败");
                }
            }
            catch (Exception ee)
            {
                COMMON.ClsCommon.ErrorMassage(ee.Message);
                bRtn = false;
            }
            return bRtn;
        }

        [DllImport("advapi32.DLL", SetLastError = true)]
        public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用stata画出文献中常见的安慰剂检验图
    • ¥15 c语言链表结构体数据插入
    • ¥40 使用MATLAB解答线性代数问题
    • ¥15 COCOS的问题COCOS的问题
    • ¥15 FPGA-SRIO初始化失败
    • ¥15 MapReduce实现倒排索引失败
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型