编程语言
首页 > 编程语言> > c#验证域账号和密码

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;
                }
            }
        }

标签:pw,验证,c#,账号,results,item,foreach,new,string
来源: https://www.cnblogs.com/marit/p/16364762.html