I'm trying to create a login prompt. I have a xml file like this:
<Users>
<User Name=User1 Password=Pass1/>
<User Name=User2 Password=Pass2/>
</Users>
How to find i开发者_JAVA技巧f a UserName which is entered in a textbox exists in the file and find if the password is entered correctly? If there's a better way than using xml, can you provide any info?
Here's a couple places to peruse:
http://weblogs.asp.net/psheriff/archive/2009/10/27/create-a-login-window-in-wpf.aspx
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/7723452e-9128-4ed7-b0ad-db7a1e3b3af7/
There are many ways to do this, but are you saying that you're storing the password as plain text in an XML file? This isn't a good practice. You should at least be encrytping the password.
If you insist on doing it this way, check out LINQ To XML, which you could use to read the password from the correct user, and compare it to the one input.
With regards your question of doing this a better way, is this a desktop app or web app?
This code is in VB 6 (only InStr function) but you can understand it or convert it to VB.Net if you have Artinsoft converter. Try this (without any XML helpers):
Function CheckLoginPassword( UserName, Password)
Dim l as string, allText as string
l="<User Name=" & UserName & " Password=" & Password & "/>"
'... HERE write code which will copy your XML's contents to variable allText
If InStr(1,allText,l)>0 then
CheckLoginPassword=True
Endif
End Function
After this you can write anywhere:
If CheckLogin (txtUserName.Text, txtPassword.Text)=true then ... else ...
精彩评论