带有 XML 文件的身份验证表单

我需要有关此身份验证表格的帮助。


这是xml文件:


  <USER_LIST>

    <user>

      <name>giulio</name>

      <password>prova</password>

    </user>

    <user>

      <name>giulia</name>

      <password>prova1</password>

    </user>

    <user>

      <name>renato</name>

      <password>zero</password>

    </user>

  </USER_LIST>

这是我写的代码:


private void button4_Click(object sender, EventArgs e)

        {


            XmlDocument doc = new XmlDocument();


            doc.Load("dati.txt");


            foreach (XmlNode node in doc.SelectNodes("//user"))

            {

                String User = node.SelectSingleNode("name").InnerText;

                String Pass = node.SelectSingleNode("password").InnerText;


                if (User == textBox1.Text && Pass == textBox2.Text)

                {

                    button1.Visible = true;

                    dlt_btn.Visible = true;

                    button3.Visible = true;

                    button3.Visible = true;

                    button5.Visible = true;

                    return;

                }


                else

                {

                    MessageBox.Show("Invalid Username or Password!");

                }

            } 

        }

但是像这样,例如,如果我使用名称“renato”和密码“零”登录,它会返回两次消息框“用户名或密码无效!” 第三次它显示所需的按钮。我知道为什么,但我想不出另一种方法来做到这一点。这是我的第一个项目,我像昨天一样开始编码,所以如果问你这样的愚蠢事情,我很抱歉。


提前谢谢你的帮助!


慕森卡
浏览 196回答 3
3回答

慕码人2483693

我想这只是为了学习目的或应该保持简单的作业,否则根本不安全。您不需要使用循环。目前,您的循环循环会一一检查所有节点,对于与给定用户/密码不匹配的每个节点,都会显示消息框,这就是为什么您会看到消息框,直到循环到达正确的用户/密码为止。在不使用循环的情况下,您可以通过这种方式轻松检查给定的用户/密码是否存在于您的 xml 文件中:var userName = userNameTextBox.Text;var password = passwordTextBox.Text;var match = System.Xml.Linq.XElement.Load(@"d:\users.xml")&nbsp; &nbsp; .Elements("user")&nbsp; &nbsp; .Where(x => x.Element("name")?.Value == userName &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.Element("password")?.Value == password)&nbsp; &nbsp; .Any();然后如果match不是真的,你可以显示消息框。

一只斗牛犬

问题是每次检查 XML 中不匹配的条目时都会显示消息框。对代码进行最少更改的最简单方法如下:private void button4_Click(object sender, EventArgs e){&nbsp; &nbsp; XmlDocument doc = new XmlDocument();&nbsp; &nbsp; doc.Load("dati.txt");&nbsp; &nbsp; bool found = false;&nbsp; &nbsp; foreach (XmlNode node in doc.SelectNodes("//user"))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String User = node.SelectSingleNode("name").InnerText;&nbsp; &nbsp; &nbsp; &nbsp; String Pass = node.SelectSingleNode("password").InnerText;&nbsp; &nbsp; &nbsp; &nbsp; if (User == textBox1.Text && Pass == textBox2.Text)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (found)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; button1.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; dlt_btn.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; button3.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; button3.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; button5.Visible = true;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Invalid Username or Password!");&nbsp; &nbsp; }}&nbsp;我们创建一个名为的变量found并将其设置为false. 这是为了确保如果 XML 为空或没有匹配项,我们将无法通过检查。然后我们遍历结果并设置found = true是否找到匹配项。我们调用 break 来跳出循环。循环完成后,我们检查局部变量是否为真:if (found)这是简写 if (found == true)如果这是真的,那么我们会像以前一样启用您的按钮。如果它不正确,那么我们将显示错误消息。它只会显示一次错误消息。

繁花不似锦

这是一个解决方案,有效:private void button4_Click(object sender, EventArgs e){&nbsp; &nbsp; string username;&nbsp; &nbsp; string password;&nbsp; &nbsp; string CurrentUser = "";&nbsp; &nbsp; string CurrentPwd = "";&nbsp; &nbsp; bool LoginStatus = false;&nbsp; &nbsp; username = textBox1.Text;&nbsp; &nbsp; password = textBox2.Text;&nbsp; &nbsp; XmlDocument xmxdoc = new XmlDocument();&nbsp; &nbsp; xmxdoc.Load("dati.txt");&nbsp; &nbsp; XmlNodeList xmlnodelist = xmxdoc.GetElementsByTagName("user");&nbsp; &nbsp; foreach (XmlNode xn in xmlnodelist)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; XmlNodeList xmlnl = xn.ChildNodes;&nbsp; &nbsp; &nbsp; &nbsp; foreach (XmlNode xmln in xmlnl)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xmln.Name == "name")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xmln.InnerText == username)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurrentUser = username;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xmln.Name == "password")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (xmln.InnerText == password)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CurrentPwd = password;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if ((CurrentUser != "") & (CurrentPwd != ""))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LoginStatus = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (LoginStatus == true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; button1.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; dlt_btn.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; button3.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; button3.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; button5.Visible = true;&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Invalid Username or Password!");&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP