【WinForm】操作web.config验证用户

最近两天在做操作web.config 的winform工具,做起来不是很顺。
先说一下需求,就是通过web。config 来判断用户时候有权限登录。
然后呢,通过winform 小工具来操作(增删改)webconfig里面的用户

第一:设置web.config 能够用来判断用户是否能登录

   在system.web 的节点下 添加如下节点,用于权限验证。

<authentication mode="Forms">
      <forms name="PRO" loginUrl="login.aspx" defaultUrl="~/Login.aspx" protection="All" timeout="60" path="/">
        <credentials passwordFormat="Clear">
          <user name="ABC" password="123" />
          <user name="aaa" password="aaa" />
          <user name="rui" password="rui" xmlns="" />
        </credentials>
      </forms>
    </authentication>

        Login.cs---------------->

View Code
protected void btnLogin_Click(object sender, EventArgs e)
    {
        string name = txtUserName.Text.Trim();
        string pwd = txtPwd.Text.Trim();
        string filePath = Server.MapPath("web.config");
        //加载XML
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filePath);
        //定义命名空间
        XmlNode root = xmlDoc.DocumentElement;
        XmlNamespaceManager xnsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        xnsmgr.AddNamespace("ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
        //查询节点
       
        XmlNodeList xmlList = root.SelectNodes("/ns:configuration/ns:system.web/ns:authentication/ns:forms/ns:credentials", xnsmgr)[0].ChildNodes;
        bool Flag = false;
        for (int i = 0; i < xmlList.Count; i++)
        {
            if (xmlList[i].Attributes["name"].Value == name && xmlList[i].Attributes["password"].Value == pwd)
            {
                Flag = true;
                //跳转
                Response.Redirect("~/Default.aspx");
            }
        }
        if (!Flag)
        {
            MsgBox("登录失败");
        }
}

  第二 小工具实现用户的增删改

           界面如下:

         

   主要代码实现如下:

     Add -------> 向<credentials>节点下添加子节点<user>

View Code
string user = txtName.Text;
            string pwd = txtPwd.Text;
            //加载XML
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            XmlNode root = doc.DocumentElement;
            XmlNamespaceManager xnsm = new XmlNamespaceManager(doc.NameTable);
            xnsm.AddNamespace("ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
            //添加前检查是否重复
            XmlNodeList xmlList = root.SelectNodes("/ns:configuration/ns:system.web/ns:authentication/ns:forms/ns:credentials", xnsm)[0].ChildNodes;
             for (int i = 0; i < xmlList.Count; i++)
             {
                 if (xmlList[i].Attributes["name"].Value == user)
                 {
                     MessageBox.Show("已经存在相同的用户名,请更换!");
                     return;
                 }
             }
            //查询到所需结果的父节点
            XmlNode nodeP = root.SelectNodes("/ns:configuration/ns:system.web/ns:authentication/ns:forms/ns:credentials", xnsm)[0];
            
            XmlElement xmlem = doc.CreateElement("user");

            XmlAttribute nodeAttribute = doc.CreateAttribute("name");//创建属性
            XmlAttribute nodeAttribute2 = doc.CreateAttribute("password");

            xmlem.Attributes.Append(nodeAttribute);//把属相添加到节点中
            xmlem.Attributes.Append(nodeAttribute2);

            xmlem.SetAttribute("name", user);//属相名和值对应 
            xmlem.SetAttribute("password", pwd);

            nodeP.AppendChild(xmlem);//子节点添加到父节点中
            doc.Save(filePath);
            BindData();
            MessageBox.Show("添加成功");
            txtName.Text = "";
            txtPwd.Text = "";


     Del--------> 删除选中行(dataGridView)

View Code
int row = this.dataGridView1.CurrentCell.RowIndex;
            string name = dataGridView1.Rows[row].Cells["UserName"].Value.ToString();
            string pwd = dataGridView1.Rows[row].Cells["PassWord"].Value.ToString();

            if (dataGridView1.CurrentRow != null)
            {
                if (MessageBox.Show("确定要删除该行数据吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    try
                    {
                        xmlDoc = new XmlDocument();
                        xmlDoc.Load(filePath);
                        XmlNode root = xmlDoc.DocumentElement;
                        XmlNamespaceManager xnsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                        xnsmgr.AddNamespace("ns", "http://schemas.microsoft.com/.NetConfiguration/v2.0");
                        XmlNodeList xmlList = root.SelectNodes("/ns:configuration/ns:system.web/ns:authentication/ns:forms/ns:credentials", xnsmgr)[0].ChildNodes;
                        for (int i = 0; i < xmlList.Count; i++)
                        {
                            if (xmlList[i].Attributes["name"].Value == name && xmlList[i].Attributes["password"].Value == pwd)
                            {
                                //删除
                                xmlList[i].ParentNode.RemoveChild(xmlList[i]);
                                xmlDoc.Save(filePath);
                                MessageBox.Show("删除成功!");
                                break;
                            }
                        }
                        BindData();
                    }
                    catch (Exception ex)
                    { MessageBox.Show(ex.Message); }
                }
            }

    Update----->目前没做,先用 删除后在添加 做法来实现

以上,就是我这两天的做法的实现和一些总结,希望能对看到的人有所帮助,同时也是自己的一次笔记! Write By -Ruicky

原文地址:https://www.cnblogs.com/ruicky/p/2721267.html