c#验证域账号和密码

1、验证程序界面

2、代码

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            List<string> pws = new List<string>();
            foreach (var item in textBox2.Lines)
            {
                if (!string.IsNullOrEmpty(item.Trim()))
                {
                    pws.Add(item);
                }
            }
            StringBuilder results = new StringBuilder();
            foreach (var item in textBox1.Lines)
            {
                if (!string.IsNullOrEmpty(item.Trim()))
                {
                    string account = item.Trim();
                    foreach (string pw in pws)
                    {
                        if (CheckAd(account,pw))
                        {
                            results.AppendLine(string.Format("{0},{1}", account, pw));
                        }
                    }
                }
            }
            File.WriteAllText("check_users.csv", results.ToString(), Encoding.GetEncoding("GB2312"));
            MessageBox.Show("完成!");
            button1.Enabled = true;
        }
        private bool CheckAd(string user, string pw)
        {
            PrincipalContext adcontext = new PrincipalContext(ContextType.Domain);
            using (adcontext)
            {
                if (adcontext.ValidateCredentials(user,pw))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

3、改成异步处理,可以看到进度

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            Thread t = new Thread(run);   
            t.IsBackground = true;       
            t.Start();                 
            
        }
        public delegate void UpdateUIDelegate(int count, int total);   
        private void showprocess(int count, int total)                 
        {
            this.label1.Text = string.Format("已处理{0}个账号,一共{1}个",count.ToString(),total.ToString()); 
        }
        /// <summary>
        /// 耗时操作
        /// </summary>
        private void run()
        {
            List<string> pws = new List<string>();
            foreach (var item in textBox2.Lines)
            {
                if (!string.IsNullOrEmpty(item.Trim()))
                {
                    pws.Add(item);
                }
            }
            StringBuilder results = new StringBuilder();
            int count = 0;
            foreach (var item in textBox1.Lines)
            {
                if (!string.IsNullOrEmpty(item.Trim()))
                {
                    string account = item.Trim();
                    foreach (string pw in pws)
                    {
                        if (CheckAd(account, pw))
                        {
                            results.AppendLine(string.Format("{0},{1}", account, pw));
                        }
                    }
                }
                count++;
                this.Invoke(new UpdateUIDelegate(showprocess),new object[]{ count,textBox1.Lines.Length });
            }
            File.WriteAllText("check_users.csv", results.ToString(), Encoding.GetEncoding("GB2312"));
            MessageBox.Show("完成!");
            button1.Enabled = true;
        }
        private bool CheckAd(string user, string pw)
        {
            PrincipalContext adcontext = new PrincipalContext(ContextType.Domain);
            using (adcontext)
            {
                if (adcontext.ValidateCredentials(user,pw))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
posted @ 2022-06-10 21:44  shenshu  阅读(448)  评论(0编辑  收藏  举报