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

时间:2018-08-15 15:00:04

标签: c# xml winforms

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

这是xml文件:

// get customer data that has been handled
function callHandledCustomer() {
  $.ajax({
    type: "GET",
    url: rootUrl + "/api/chat/getHandledCustomer",
    dataType: "json",
    success: function(response){
      $.each(response, function(i, customer) {
        console.log(customer[0][0].name); // get name
      });
    }

  });
}

这是我写的代码:

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

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

谢谢您的帮助!

3 个答案:

答案 0 :(得分:1)

我想这只是出于学习目的,或者应该保持简单的作业,否则根本不安全。

您不需要使用循环。当前,您的循环循环逐个检查所有节点,并针对与给定用户/通行证不匹配的每个节点显示消息框,这就是为什么您看到消息框直到循环到达正确的用户/通行证的原因。

不使用循环,您可以通过以下方式轻松检查给定的用户/密码是否存在于xml文件中:

var userName = userNameTextBox.Text;
var password = passwordTextBox.Text;
var match = System.Xml.Linq.XElement.Load(@"d:\users.xml")
    .Elements("user")
    .Where(x => x.Element("name")?.Value == userName &&
                x.Element("password")?.Value == password)
    .Any();

然后,如果match不正确,则可以显示消息框。

答案 1 :(得分:0)

问题是,每次您检查XML中不匹配的条目时,都会显示消息框。

以最小的代码更改来完成此操作的最简单方法是这样的:

private void button4_Click(object sender, EventArgs e)
{
    XmlDocument doc = new XmlDocument();
    doc.Load("dati.txt");

    bool found = false;

    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)
        {
            found = true;
            break;
        }
    }

    if (found)
    {
        button1.Visible = true;
        dlt_btn.Visible = true;
        button3.Visible = true;
        button3.Visible = true;
        button5.Visible = true;
    }
    else
    {
        MessageBox.Show("Invalid Username or Password!");
    }
} 

我们创建一个名为found的变量并将其设置为false。这是为了确保如果XML为空或没有匹配项,我们将通过检查。

然后,我们遍历结果,如果找到匹配项,则设置found = true。我们称Break为打破循环。

循环完成后,我们将检查本地变量是否为真:

if (found)

这是if (found == true)

的简写

如果是真的,我们将像以前一样启用您的按钮。如果不正确,那么我们将显示错误消息。

它只会显示一次错误消息。

答案 2 :(得分:0)

这是一种可行的解决方案:

private void button4_Click(object sender, EventArgs e)
{
    string username;
    string password;
    string CurrentUser = "";
    string CurrentPwd = "";
    bool LoginStatus = false;

    username = textBox1.Text;
    password = textBox2.Text;

    XmlDocument xmxdoc = new XmlDocument();
    xmxdoc.Load("dati.txt");
    XmlNodeList xmlnodelist = xmxdoc.GetElementsByTagName("user");

    foreach (XmlNode xn in xmlnodelist)
    {
        XmlNodeList xmlnl = xn.ChildNodes;
        foreach (XmlNode xmln in xmlnl)
        {
            if (xmln.Name == "name")
            {
                if (xmln.InnerText == username)
                {
                    CurrentUser = username;
                }
            }
            if (xmln.Name == "password")
            {
                if (xmln.InnerText == password)
                {
                    CurrentPwd = password;
                }
            }
        }
        if ((CurrentUser != "") & (CurrentPwd != ""))
        {
            LoginStatus = true;
        }
    }
    if (LoginStatus == true)
    {
        button1.Visible = true;
        dlt_btn.Visible = true;
        button3.Visible = true;
        button3.Visible = true;
        button5.Visible = true;
        return;
    }
    else
    {
        MessageBox.Show("Invalid Username or Password!");
    }
}