C#中關於用戶名和密碼的驗證問題。


C#中關於用戶名和密碼的驗證問題。

 

本次練習的目的是使用LinQ to XML,正則表達式,明天在這個基礎上練習使用序列化和反序列化,繼續加點兒小功能。

首先,這是一個窗體程序,設計如下:

存放用戶名和密碼的XML如下:

實現的代碼如下:

復制代碼
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 using System.Text.RegularExpressions;
11 using System.Xml;
12 using System.Xml.Linq;
13 
14 namespace CheckInfo
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21         }
22 
23         private void textBox1_TextChanged(object sender, EventArgs e)
24         {
25             if (textBox1.Text == "請輸入用戶名,格式:qarootdc\\jqhuang")
26             {
27                 textBox1.Text = "";
28             }
29 
30         }
31 
32         private void textBox2_TextChanged_1(object sender, EventArgs e)
33         {
34             if (textBox2.Text == "請輸入密碼")
35             {
36                 textBox2.Text = "";
37             }
38         }
39 
40         private void button1_Click(object sender, EventArgs e)
41         {
42             if (isValidUserName(textBox1.Text) == false)
43             {
44                 MessageBox.Show("用戶名格式不正確!請重新輸入!");
45                 textBox1.Text = "";
46             }
47             else 
48             {
49                 //用戶名格式正確.
50                 CheckUserAndPwd(textBox1.Text,textBox2.Text);
51             }
52         }
53 
54         private void CheckUserAndPwd(string username, string pwd)
55         {
56             //讀取UserInfo.xml檢測user是否存在
57             XDocument userInfo = XDocument.Load(@"C:\Users\jqhuang\Desktop\UserInfo.xml");
58             var result = from userElement in userInfo.Element("System").Element("users").Elements() where userElement.Element("username").Value.ToString() == textBox1.Text.ToString() select userElement.Element("pwd").Value;
59             if (result != null)
60             {
61                 foreach (var password in result)
62                 {
63                     if (password == pwd)
64                     {
65                         MessageBox.Show("用戶名和密碼匹配成功!");
66                     }
67                     else
68                     {
69                         MessageBox.Show("用戶名和密碼不匹配,請重新輸入密碼");
70                         textBox2.Text = "";
71                     }
72                 }
73             }
74             else 
75             {
76                 MessageBox.Show("您輸入的用戶不存在!");
77             }
78         }
79 
80         bool isValidUserName(string userName) 
81         {
82             return Regex.IsMatch(userName,@"^.+\\.+$");
83         }
84     }
85 }
復制代碼

運行效果圖如下——

1、用戶名不存在的情況:

/

 

2、用戶名和密碼不匹配的情況:

 

3、用戶名格式不正確的情況(用正則表達式驗證):

 

4、用戶名和密碼匹配成功的情況:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM