I'm wishing to provide a login facility in C# windows application, maximum 20 users might be there so not a huge database is required for the WinApplication.
Kindly suggest me how should I go ahead with this idea of mine. Sample code or a help link (url) would be really appreciated.
I tried to fetch & compare the textbox.text value with the xls cell value but sadly I'm not able to do it. So the winform is not able to check the password cell next to the username cell.
1556
you can use the xls cell value correctly even by getting it from the OLEDB or by using the Office library. beside that you can store the users/passwords in a normal file by adding them as binary or text and encrypting it.
412
hey muster, U forgot that I'm a newbie, I'm just learning to read & compare the data in my windows forms. Encrypting and decrypting is a later stage, 1st of all I need to know how will my code understand & find the particular username in the file? Then how will the code get to know the password of THAT username which is entered by the user in my windows application (winform)? I'm not able to make a logic of this....:(

20 answers
I would suggest storing the logins as an encrypted CSV file then you can read it into a table. I was doing doing something similar to this and Vulpes had an answer that worked perfectly... (note the file can have any extension you want not just CSV. I used a file name and extension that was not obvious that it had sensitive data in it.
http://www.debugging.com/bug/22860
answered 2 years ago by:
196
412
I'm not a professional, I'm just a learner who's leaning on my own slow pace without any guidance/professional help. So its not so easy for me to go along with the pace/speed of u all here. :) My prob is that I'm not able to write/get a code/logic of the program where it will check the textbox.text (username) inside an xls file... then I have to figure out the code which will check the stored password of that particular username.
I would make a text file for each user with it's personal data and i would save it inside a folder after encrypting it (just like i did in my app: http://davidevitelaru.com/guardian.php )
This way you can have as many users as you want.
Also, to encrypt the text files you could use one of my class libraries:http://davidevitelaru.com/resources/DavidsoftCryptorEngine.dll
Help file: http://davidevitelaru.com/resources/DavidsoftCryptorEngine.pdf
answered 2 years ago by:
181
412
hey davide, Passwrod manager is a wonderful application. I liked the way it interacts with the user. Thx for the answer, but the prob is that I don't know the logic of the code that will read/compare the row value in an external file to check for username and its respective password stored. :)
hey mate, sorry for the late response. I'll start with the easiest way to read/write from a file in order to get/store the username and password in a text format way. first thing is to create your own format, in other words lets say the file stores data in the following format: username,password.username1,password1.username2,password2.
so as you see when you want to read the file you loop through string you have in order to get the username and password. Beside that the username and password must not contain , and . since they are used in the format. to do this code write it as follows:
make a class called credential that consists of two strings (username,password)
make a function that reads all the credentials returning them in a list of credentials objects and a function that takes all the credential objects and write them to a file in a text of specific format.
Considering encryption, make any encryption function you like for example make a simple one that increment the ascii code of the data by 2 when writing and decrement by 2 when reading. e.g.
then to do the encryption when writing credentials just call the Encrypt method before string the data:
string data = "";
for (int i = 0; i < credits.Length; i++)
data += credits[i].Username + "," + credits[i].Password + ".";
data=Encrypt(data);
System.IO.File.WriteAllText(fileName, data);
and when decrypting the data do this when the data is read:
string data=System.IO.File.ReadAllText(fileName,System.Text.Encoding.Default);
data=Decrypt(data);
hope the logic is clear now.
answered 2 years ago by:
1556
1556
also let the filename be of extension .txt in order to read it easily in the notepad that's before encryption and then check how it looks after implementing the encryption.
412
Wooo man...... 80% of the code looks like an piece of Da Vinci code. I think now after getting the dll file ready I can now do some R&D on it, so that I can figure out how I can use the dll file in my application. I'm surely able to understand the logic workng behind the code sample, but to be honest I can never figureout this code even if I KNEW this logic. How to use this credential.dll???? I'm sorry to sound like a dumbo but... :( I think I need a real praticle kinda tutorial to learn more, any sugestions plz?
I think i made things worse from your comment. But to be honest i only wrote it in order to explain how some things work. I'll start with a simple thing to do.
For example try this:
1) Make a form and add a textbox of name (txtMyData) and two buttons of names (btnSave,btnLoad) in it.
2) The first button should save the value found in the textbox in a file called myFile.txt in C and the second button should retrieve whatever data found in the file to the textbox.
3) In the save button click event handler write this:
System.IO.File.WriteAllText("C:\\myFile.txt",txtMyData.Text); this stores the text in the textbox to that file.
4) In the load button click event handler write this:
string data=System.IO.File.ReadAllText("C:\\myFile.txt");
this.txtMyData.Text = data;
if you do this code and understand it, then start with the second step which is making your text format as i explained in the answer above but this is all about string parsing and have nothing to do with file reading or writing, just slicing a bunch of character according to your own readings. Like for example searching where the comma or dot is found and adding the text before a comma to the list of usernames and the text before a dot to the list of passwords. in this way you have the usernames and passwords of every user and you can loop through the users list to check the password. For example lets say you wrote in the text file this: user1,pass1.user2,pass2. and then you saved this file by pressing the save button. to parse this text you can make a parsing function (i think i won't use split function coz i think it got u confused). So after reading the file, the parsing function is called.
5) To implement the parsing function you can reserve two arrays (better using List) of strings in the form class for example:
6) Make the parse function that takes the string in the textbox and parse it according to the format we discussed for example try writing in the textbox this "gsacc,passmac.musteracc,mpass.":
7) After you make the parse function add two textboxes 1 for username of name (txtUser) and 1 for password of name (txtPass) and a button of name (btnLogin) to check.
8) In the btnLogin click event handler call a function Authenticate to check if the entered username/password is acceptable.
9) To check, you can loop through the usernames strings to get the username you need and the index of the username should by default be the index of the password and in this way you retrieve the password for example write this function:
hope this is clear now and not da vinci code :P
answered 2 years ago by:
1556
Hey Muster,
This was really a wonderful piece of information, sure this was not a vinci code. Now I'm reading & understanding the code, I'm now trying to club it together so that I can make a template for login.
Then I may be ab;e to use this template code in all other applications where I might be required to have a login feature for my application.
Thx so much for this wonderful piece code and the explaination. Now I'll try to learn to make a template out of this information.
I'm not able to add comments to ur posts, I had a wonderful idea that if I'm able to fetch usernames from the txt file and display them in a listbox on the login page then it would be a wonderful thing.
I just wanted to ask that as our details are in a single line of a txt file.... how can I display "usernames" which are already present in that txt file? :)
answered 2 years ago by:
412
after you read the data from a text file and parse it according to its format, then you can show the usernames. for example add a label and loop through your username list and show them.
answered 2 years ago by:
1556
I'm facing error in two places:
1. In addButton_click function line "CR.WriteLoginFile(Path,CR[]credits);", and
2. In FillUsernames function line "usernames.Length" saying does not contain defination of length.
I'm sharing the code for everybody, but its not working properly right now.
Code of Form1.cs
Code of credential.cs
answered 2 years ago by:
412
1556
You mixed two codes together instead of holding on 1 code. The old code was written as a separate code independant of your form but the second one is fixed with the form you have as a tutorial. I"m out of time and can't write u the answer atm. but my advice is just stick to the second code i wrote you and take the Encrypt/Decrypt functions from the first code and don't mix them up because they use different ways unless you understand the first code where you'll be able to use it without a problem and attach it to ur form.
Ohh I c.... Okie let's suppose if I use just the 2nd part of the code then the code will look somewhat like this:
But still the defination of "Length" is giving the error, 'System.Collections.Generic.List<string>' does not contain a definition for 'Length'. So I'm not able to populate the list of users, moreover I prefer it to be displayed in a listbox instead of a label.text....What am I missing here????
Form1.cs
Take ur time, no hurries... reply at ur convenience. Atleast u didn't forgot me like vulpes did, I just wish that I had not offended him unintentionally.... :)
answered 2 years ago by:
412
Sorry to muscle in on your answer, muster, but we can't have gsvirdi thinking that I've forgotten him ;)
Unlike arrays and stringbuilders which have Length properties, most other collections (including generic Lists) have a Count property instead.
However, if you're going to load the usernames into a listbox rather than a label, then you won't need it:
answered 2 years ago by:
17279
1556
don't worry, anything that would help gsvirdi is great. and for gsvidri the length was the problem, a typing mistake, and it should be usernames.Count instead of length :)
I made a mistake again.........
Now the data is not getting added into the txt file, nor the Fillusername is populating the listbox items with usernames on Form1_load. Looks like I can stuff-in errors inside Expert's help too.
I never knew that helping/saying is muscling around in-here, I had always thought that I should be saying whatever i know or whatever I want to know. So vulpes u should not be thinking like this, it was only muster's help that gave a kind of "light at the end of the tunnle" for me. Otherwise I had started believing that I'm not welcome here in new house (debugging) so was wishing if I can get my csharp group back to help me create errors. :)
answered 2 years ago by:
412
The error lies in the load first of all, when you are loading the file and decrypting it, you are not Parse it before filling the usernames list, and the fillusername method is showing empty because the list is not already filled.
beside this, muscling should be taken into consideration depending on the situation you are answering, but vulpes answered in a place where i was off and couldn't give a clear answer so it depends when and where to answer. hope that i'm right with that, and feel free to ask about any problem in here.
answered 2 years ago by:
1556
Webamster have to do something here... I'm not able to add comments, nor I'm able to vote the answer. Something's wrong....
Anyway I was saying: Thx muster for ur reply I had already did that. I'm parsing it in the FillUsernames code
And another problem is that the data is not getting exported to the file. If I try to add the username & password without symbols , and . like:
Then the file remains blank..... But now if I add those symbols
Then the data is getting exported into the file. But the data is not encrypted and can be viewed easily.
But in all these cases.... the previous data in the file is getting deleted & if the data is added... then it remains un-encrypted also. So it means that I had made a blunder somewhere.
I've also observed that the FillUserNames() is not gathering the list of usernames from the .txt file. What I wanted was to display the list of usernames presently existing inside the .txt file which is somewhere in the shared network drive. :(
Boss gave me some work to do... so I'll look into the code from home at night.
I never saw that any of u is intentionally trying to prove the other person wrong or argue without any good reason. Moreover everybody here is a grownup & mature person (except me) so from my point-of-view I don't think "muscling" could ever come between us :)
answered 2 years ago by:
412
I made a mistake in not reading ur code clearly again, to add a username and password simply add it to the list of usernames and password you have:
when adding a user
also the same thing is done when you remove the user, you only have to remove the username and password form the list and then fill the data string with the needed format.
now concerning the read part in the form load, you messed the format alot by making it in two textboxes, just remake your parse function again keeping ur current format which is un1,un2,un3,un4,pass1.pass2.pass3.pass4.
my advice "which you won't like" is to start again and understand it line by line.
answered 2 years ago by:
1556
Sure...
No fresher would like this devastating advice, but as I see that I don't have any better option as a "Learner". So I think I would luv to listen to ur advice and start from the scratch.
As I see that I made a mistake of trying to merge two things into the same form:
1. the User login &
2. the admin form (add/remove users).
I was thinking that the admin can also use those username & passwrod textbox for "user maintenance" purpose also. I just wanted a small utility which can work on the network. Hmm... something like this:
Form1.Designer.cs
Okie now let me start again......
answered 2 years ago by:
412
1556
as a beginning split these ofcourse into two forms one for the admin to add/remove and one for login (which is usually done in most of the applications) where the user login and if he is an admin he can access the admin form.
412
I was able to get this code to work for login: [code] public bool findData(string data) { FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); string s1 = sr.ReadLine(); bool check = false; while (s1 != null) { s2 = s1.Split(','); if (data == "1") { if (s2[0].Equals(textBox1.Text.ToLower()) && s2[1].Equals(textBox2.Text)) { check = true; break; } } else listBox1.Items.Add(s2[0].ToString()); s1 = sr.ReadLine(); } return check; } [/code] Contents of the txt file is: admin,Admin-123 guest,guest gsvirdi,Gsv123 Now I need to figure out the Admin action: [b]Add User[/b]" and a "[b]Remove User[/b]"
1556
make another thread for this, it would be good