blah blah blah is here! blah blah » Close

up0down
link

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.

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

gsvirdi
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....:(

last answered one year ago

20 answers

up0down
link

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

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

up1down
link

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

gsvirdi
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. :)

up1down
link

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)

class Credential
{
public string Username;
public string 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.
//Write all the credentials we have in the array of credits to a file using our format username,password.
void WriteLoginFile(string fileName, Credential[] credits)
{
string data = "";
for (int i = 0; i < credits.Length; i++)
data += credits[i].Username + "," + credits[i].Password + ".";
System.IO.File.WriteAllText(fileName, data);
}

//Read the file and get all the username/password objects from the text read
Credential[] ReadLoginFile(string fileName)
{
string data=System.IO.File.ReadAllText(fileName,System.Text.Encoding.Default);
//in order to ease things up and read format in a fast way try using the string split function in order to get array of string when the seperator character such as . is reached
string[] credentialsText = data.Split('.', StringSplitOptions.RemoveEmptyEntries);
//splitting the data e.g. muster,mypass@acc%.mustertest,@gi#5. will retrieve an array of strings of two values: first is "muster,mypass@acc%" and second is "mustertest,@gi#5". The removeemptyenries remove the emptyentries of split such as the last string after the last . found in data
Credential[] credits=new Credential[credentialsText.Length];//create an array of credentials of length equal to the number of credentialsText strings.
for (int i = 0; i < credentialsText.Length; i++)
{
Credential credit = new Credential();
credit.Username = credentialsText[i].Split(',')[0];//get the first string before the , which is the username
credit.Password = credentialsText[i].Split(',')[1];//get the string after the , which is the password
credits[i] = credit;
}
return credits;
}


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.

string Encrypt(string data)
{
string newData="";
for(int i=0;i<data.Length;i++)
{
int ascii=(int)data[i];
ascii= ascii+2;
newData+= (char)ascii;
}
return newData;
}

string Decrypt(string data)
{
string newData="";
for(int i=0;i<data.Length;i++)
{
int ascii=(int)data[i];
ascii= ascii-2;
newData+= (char)ascii;
}
return newData;
}


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.

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

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

up0down
link

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:

List<string> usernames=new List<string>();
List<string> passwords=new List<string>();


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.":
void Parse()
{
usernames.Clear();//empty the usernames list
passwords.Clear();//empty the passwords list
string temp="";
//we need to loop through the whole text in txtMyData textbox and store the text before any comma to usernames and the text before any dot to passwords
for(int i=0;i<txtMyData.Text.Length;i++)
{
if(txtMyData.Text[i]==',')//if the char is comma then store the temp to usernames list
{
usernames.Add(temp);
temp="";
}
else if(txtMyData.Text[i]=='.')//if the char is dot then store the temp to passwords list
{
passwords.Add(temp);
temp="";
}
else
temp+=txtMyData.Text[i].ToString();
//store whatever text to the temp string in order to add it to the need list if the next char is comma or dot
}
//when u finish this loop the usernames and passwords list are filled with strings repectively.
}

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:
bool Authenticate()
{
for(int i=0;i<usernames.Count;i++)
{
if(usernames[i]==txtUser.Text)//check if the username entered is found in the list of usernames
{
if(passwords[i]==txtPass.Text)
return true;//if the username is found and the passwords (of same id) is found then the authentication was successfull.
}
}//if the username or the password is not found or are not compatible the authenticate fails and return false
return false;
}



hope this is clear now and not da vinci code :P

up0down
link

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? :)

up0down
link

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.

void FillUsernames()
{
this.lblAllUsers.Text="";
for(int i=0;i<usernames.Length;i++)
{
this.lblAllUsers.Text += usernames[i] +"\n";
}
}

up0down
link

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace UserAdmin
{
public partial class Form1 : Form
{
string Path = "W:\\gsvirdi\\credz.lnt";
string data = "";

Credential CR = new Credential();
public Form1()
{
InitializeComponent();
}

List<string> usernames = new List<string>();
List<string> passwords = new List<string>();

public void Parse()
{
usernames.Clear(); //empty the usernames list
passwords.Clear(); //empty the passwords list
string temp = "";
for (int i = 0; i < this.textBox1.Text.Length; i++)
{
if (textBox1.Text[i] == ',') //if the char is comma then store the temp to usernames list
{
usernames.Add(temp);
temp = "";
}
else
{
temp += this.textBox1.Text[i].ToString();
//store whatever text to the temp string in order to add it to the need list if the next char is comma or dot
}
} //when u finish this loop the usernames and passwords list are filled with strings repectively.

for (int j = 0; j < this.textBox1.Text.Length; j++)
{
if (textBox2.Text[j] == '.') //if the char is dot then store the temp to passwords list
{
passwords.Add(temp);
temp = "";
}
else
{
temp += this.textBox2.Text[j].ToString();
//store whatever text to the temp string in order to add it to the need list if the next char is comma or dot
}
}
}

bool Authenticate()
{
for(int i=0;i<usernames.Count;i++)
{
if(usernames[i]==this.textBox1.Text) //check if the username entered is found in the list of usernames
{
if(passwords[i]==this.textBox2.Text)
return true; //if the username is found and the passwords (of same id) is found then the authentication was successfull.
}
} //if the username or the password is not found or are not compatible the authenticate fails and return false
return false;
}

bool kick;

private void Chk(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text != "")
{
kick = false;
}
else
{
MessageBox.Show("Password field is Blank","Error",MessageBoxButtons.OK);
kick = true;
}
}
else
{
MessageBox.Show("Username field is Blank","Error",MessageBoxButtons.OK);
kick = true;
}
}

void FillUsernames()
{
this.label5.Text = "";
Parse();
for (int i = 0; i < usernames.Length; i++)
{
this.label5.Text += usernames[i] + "\n";
}
}

private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.textBox2.Text = "";
string data = System.IO.File.ReadAllText(Path, System.Text.Encoding.Default);
data = CR.Decrypt(data);
CR.ReadLoginFile(Path);
CR.Encrypt(data);
}

private void addButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
CR.Decrypt(data);
Parse();
CR.ReadLoginFile(Path);
//CR.WriteLoginFile(Path,CR[]credits); Error here
MessageBox.Show("User information added into the database.", "Success...");
label5.Text = "";
Form1_Load(sender, e);
CR.Encrypt(data);
}
}

private void remButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
CR.Decrypt(data);
Parse();
CR.ReadLoginFile(Path);
// Remove function has to be added.
MessageBox.Show("User information added into the database.", "Success...");
label5.Text="";
FillUsernames();
Form1_Load(sender, e);
CR.Encrypt(data);
}
}

private void loginButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
CR.Decrypt(data);
Parse();
Authenticate();
}
}

private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}



Code of credential.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace UserAdmin
{
class Credential
{
public string Username;
public string Password;
string data = "";

//Read the file and get all the username/password objects from the text read
public Credential[] ReadLoginFile(string Path)
{
string data = System.IO.File.ReadAllText(Path, System.Text.Encoding.Default);
//in order to ease things up and read format in a fast way try using the string
//split function in order to get array of string when the seperator character such as . is reached
char X = Convert.ToChar(StringSplitOptions.RemoveEmptyEntries);
string[] credentialsText = data.Split('.', X);
//splitting the data e.g. muster,mypass@acc%.mustertest,@gi#5. will retrieve an array of strings of two values: first is "muster,mypass@acc%" and second is "mustertest,@gi#5".
//The removeemptyenries remove the emptyentries of split such as the last string after the last . found in data
Credential[] credits = new Credential[credentialsText.Length];
//create an array of credentials of length equal to the number of credentialsText strings.
for (int i = 0; i < credentialsText.Length; i++)
{
Credential credit = new Credential();
credit.Username = credentialsText[i].Split(',')[0];
//get the first string before the , which is the username
credit.Password = credentialsText[i].Split(',')[1];
//get the string after the , which is the password
credits[i] = credit;
}
return credits;
}

//Write all the credentials we have in the array of credits to a file using our format username,password.
public void WriteLoginFile(string Path, Credential[] credits)
{
for (int i = 0; i < credits.Length; i++)
data += credits[i].Username + "," + credits[i].Password + ".";
System.IO.File.WriteAllText(Path, data);
}

//Write all the credentials we have in the array of credits to a file using our format username,password.
public void EraseLoginFile(string Path, Credential[] credits)
{

}

public string Encrypt(string data)
{
string newData = "";
for (int i = 0; i < data.Length; i++)
{
int ascii = (int)data[i];
ascii = ascii + 2;
newData += (char)ascii;
}
return newData;
}

public string Decrypt(string data)
{
string newData = "";
for (int i = 0; i < data.Length; i++)
{
int ascii = (int)data[i];
ascii = ascii - 2;
newData += (char)ascii;
}
return newData;
}
}
}

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

up0down
link

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace UserAdmin
{
public partial class Form1 : Form
{
string Path = "W:\\gsvirdi\\credz.lnt";
string data = "";

public Form1()
{
InitializeComponent();
}

List<string> usernames = new List<string>();
List<string> passwords = new List<string>();

public void Parse()
{
usernames.Clear(); //empty the usernames list
passwords.Clear(); //empty the passwords list
string temp = "";
for (int i = 0; i < this.textBox1.Text.Length; i++)
{
if (textBox1.Text[i] == ',') //if the char is comma then store the temp to usernames list
{
usernames.Add(temp);
temp = "";
}
else
{
temp += this.textBox1.Text[i].ToString();
//store whatever text to the temp string in order to add it to the need list if the next char is comma or dot
}
} //when u finish this loop the usernames and passwords list are filled with strings repectively.

for (int j = 0; j < this.textBox1.Text.Length; j++)
{
if (textBox2.Text[j] == '.') //if the char is dot then store the temp to passwords list
{
passwords.Add(temp);
temp = "";
}
else
{
temp += this.textBox2.Text[j].ToString();
//store whatever text to the temp string in order to add it to the need list if the next char is comma or dot
}
}
}

bool Authenticate()
{
for(int i=0;i<usernames.Count;i++)
{
if(usernames[i]==this.textBox1.Text) //check if the username entered is found in the list of usernames
{
if(passwords[i]==this.textBox2.Text)
return true; //if the username is found and the passwords (of same id) is found then the authentication was successfull.
}
} //if the username or the password is not found or are not compatible the authenticate fails and return false
return false;
}

bool kick;

private void Chk(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text != "")
{
kick = false;
}
else
{
MessageBox.Show("\n\tPassword field is Blank\t\n","Error",MessageBoxButtons.OK);
kick = true;
}
}
else
{
MessageBox.Show("\n\tUsername field is Blank\t\n","Error",MessageBoxButtons.OK);
kick = true;
}
}

void FillUsernames()
{
this.label5.Text = "";
Parse();
for (int i = 0; i < usernames.Length; i++)
{
this.label5.Text += usernames[i] + "\n";
}
}

public string Encrypt(string data)
{
string newData = "";
for (int i = 0; i < data.Length; i++)
{
int ascii = (int)data[i];
ascii = ascii + 2;
newData += (char)ascii;
}
return newData;
}

public string Decrypt(string data)
{
string newData = "";
for (int i = 0; i < data.Length; i++)
{
int ascii = (int)data[i];
ascii = ascii - 2;
newData += (char)ascii;
}
return newData;
}

private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.textBox2.Text = "";
string data = System.IO.File.ReadAllText(Path, System.Text.Encoding.Default);
data = Decrypt(data);
Encrypt(data);
}

private void addButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
Decrypt(data);
Parse();
//for (int i = 0; i < credits.Length; i++)
// data += credits[i].Username + "," + credits[i].Password + ".";
//data = Encrypt(data);
MessageBox.Show("User information added into the database.", "Success...");
label5.Text = "";
System.IO.File.WriteAllText(Path, data);
File.WriteAllText(Path, textBox1.Text + "," + textBox2.Text + ".");
Form1_Load(sender, e);
Encrypt(data);
}
}

private void remButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
Decrypt(data);
Parse();
MessageBox.Show("User information added into the database.", "Success...");
label5.Text="";
FillUsernames();
Form1_Load(sender, e);
Encrypt(data);
}
}

private void loginButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
Decrypt(data);
Parse();
Authenticate();
}
}

private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}


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.... :)

up0down
link

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:

void FillUsernames()
{
this.listBox1.Items.Clear();
Parse();
this.listBox1.Items.AddRange(usernames.ToArray());
}

muster
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 :)

up0down
link

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.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace UserAdmin
{
public partial class Form1 : Form
{
string path = "W:\\gsvirdi\\credz.lnt";
string data = "";

public Form1()
{
InitializeComponent();
}

List<string> usernames = new List<string>();
List<string> passwords = new List<string>();

public void Parse()
{
usernames.Clear(); //empty the usernames list
passwords.Clear(); //empty the passwords list
string temp = "";
for (int i = 0; i < this.textBox1.Text.Length; i++)
{
if (textBox1.Text[i] == ',') //if the char is comma then store the temp to usernames list
{
usernames.Add(temp);
temp = "";
}
else
{
temp += this.textBox1.Text[i].ToString();
//store whatever text to the temp string in order to add it to the need list if the next char is comma or dot
}
} //when u finish this loop the usernames and passwords list are filled with strings repectively.

for (int j = 0; j < this.textBox2.Text.Length; j++)
{
if (textBox2.Text[j] == '.') //if the char is dot then store the temp to passwords list
{
passwords.Add(temp);
temp = "";
}
else
{
temp += this.textBox2.Text[j].ToString();
//store whatever text to the temp string in order to add it to the need list if the next char is comma or dot
}
}
}


bool Authenticate()
{
for(int i=0;i<usernames.Count;i++)
{
if(usernames[i]==this.textBox1.Text) //check if the username entered is found in the list of usernames
{
if (passwords[i] == this.textBox2.Text)
return true; //if the username is found and the passwords (of same id) is found then the authentication was successfull.
}
} //if the username or the password is not found or are not compatible the authenticate fails and return false
return false;
}

bool kick;

private void Chk(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (textBox2.Text != "")
{
kick = false;
}
else
{
MessageBox.Show("\n\tPassword field is Blank\t\n","Error",MessageBoxButtons.OK);
kick = true;
}
}
else
{
MessageBox.Show("\n\tUsername field is Blank\t\n","Error",MessageBoxButtons.OK);
kick = true;
}
}

void FillUsernames()
{
this.listBox1.Items.Clear();
Parse();
for (int i = 0; i < usernames.Count; i++)
{
this.listBox1.Items.AddRange(usernames.ToArray());
}
}

public string Encrypt(string data)
{
string newData = "";
for (int i = 0; i < data.Length; i++)
{
int ascii = (int)data[i];
ascii = ascii + 2;
newData += (char)ascii;
}
return newData;
}

public string Decrypt(string data)
{
string newData = "";
for (int i = 0; i < data.Length; i++)
{
int ascii = (int)data[i];
ascii = ascii - 2;
newData += (char)ascii;
}
return newData;
}

private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.Text = "";
this.textBox2.Text = "";
data = File.ReadAllText(path, Encoding.Default);
Decrypt(data);
FillUsernames();
}

private void addButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
Parse();
string data1 = "";
string data2 = "";
for (int i = 0; i < usernames.Count; i++)
{
data1 += usernames[i] + ",";
}
for (int j = 0; j < passwords.Count; j++)
{
data2 += passwords[j] + ".";
}
data = data1 + data2;
Encrypt(data);
File.WriteAllText(path, data);
MessageBox.Show("User information added into the database.", "Success...");
Form1_Load(sender, e);
}
}

private void remButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
Decrypt(data);
Parse();
MessageBox.Show("This part is not ready to use now.", "Success...");
Form1_Load(sender, e);
}
}

private void loginButton_Click(object sender, EventArgs e)
{
Chk(sender, e);
if (!kick)
{
Decrypt(data);
Parse();
Authenticate();
if (Authenticate())
{
MessageBox.Show("Login Sucessful.", "Welcome");
}
else
{
MessageBox.Show("Login Failed.", "Error");
}
}
}

private void exitButton_Click(object sender, EventArgs e)
{
Encrypt(data);
Application.Exit();
}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
FillUsernames();
}
}
}


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. :)

up0down
link

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.

this.textBox1.Text = "";
this.textBox2.Text = "";
data = File.ReadAllText(path, Encoding.Default);
Decrypt(data);
Parse();
FillUsernames();



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.

up0down
link

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

void FillUsernames()
{
this.listBox1.Items.Clear();
Parse();
for (int i = 0; i < usernames.Count; i++)
{
this.listBox1.Items.AddRange(usernames.ToArray());
}
}


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:
Username as: guest
Password as guest

Then the file remains blank..... But now if I add those symbols
Username as: guest,
Password as guest.

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

up0down
link

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

usernames.Add(this.textBox1.Text);
passwords.Add(this.textBox2.Text);
if (!kick)
{
Parse();//why parsing?? there is no need to parse because you have everything in the usernames list.
string data1 = "";
string data2 = "";
for (int i = 0; i < usernames.Count; i++)
{
data1 += usernames[i] + ",";
}
for (int j = 0; j < passwords.Count; j++)
{
data2 += passwords[j] + ".";
}
data = data1 + data2;
Encrypt(data);//you encrypted the string and returned it but u didn't store it in a new value so it should be something like data=Encrypt(data);
File.WriteAllText(path, data);
MessageBox.Show("User information added into the database.", "Success...");
Form1_Load(sender, e);
}

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.
this.textBox1.Text = "";
this.textBox2.Text = "";
data = File.ReadAllText(path, Encoding.Default);
data=Decrypt(data);// the same thing as encrypt. it returns string and you are not using it in any way
this.textBox1.Text=data;
Parse();//the parse function needs alot of work or you might change the format to the initial format i gave u
FillUsernames();



my advice "which you won't like" is to start again and understand it line by line.

up0down
link

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

namespace UserAdmin
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.addButton = new System.Windows.Forms.Button();
this.remButton = new System.Windows.Forms.Button();
this.exitButton = new System.Windows.Forms.Button();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.loginButton = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(58, 13);
this.label1.TabIndex = 8;
this.label1.Text = "Username:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 13);
this.label2.TabIndex = 9;
this.label2.Text = "Password:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label3.Location = new System.Drawing.Point(39, 5);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(127, 17);
this.label3.TabIndex = 10;
this.label3.Text = "Manage Users...";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(76, 37);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(76, 69);
this.textBox2.Name = "textBox2";
this.textBox2.PasswordChar = 'x';
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 1;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(196, 5);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(51, 13);
this.label4.TabIndex = 11;
this.label4.Text = "User List:";
//
// addButton
//
this.addButton.Location = new System.Drawing.Point(104, 110);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(55, 23);
this.addButton.TabIndex = 3;
this.addButton.Text = "Add";
this.addButton.UseVisualStyleBackColor = true;
this.addButton.Click += new System.EventHandler(this.addButton_Click);
//
// remButton
//
this.remButton.Location = new System.Drawing.Point(167, 110);
this.remButton.Name = "remButton";
this.remButton.Size = new System.Drawing.Size(55, 23);
this.remButton.TabIndex = 4;
this.remButton.Text = "Remove";
this.remButton.UseVisualStyleBackColor = true;
this.remButton.Click += new System.EventHandler(this.remButton_Click);
//
// exitButton
//
this.exitButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.exitButton.Location = new System.Drawing.Point(230, 110);
this.exitButton.Name = "exitButton";
this.exitButton.Size = new System.Drawing.Size(55, 23);
this.exitButton.TabIndex = 5;
this.exitButton.Text = "Exit";
this.exitButton.UseVisualStyleBackColor = true;
this.exitButton.Click += new System.EventHandler(this.exitButton_Click);
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.Location = new System.Drawing.Point(277, 5);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(44, 13);
this.linkLabel1.TabIndex = 6;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "Refresh";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// loginButton
//
this.loginButton.Location = new System.Drawing.Point(41, 110);
this.loginButton.Name = "loginButton";
this.loginButton.Size = new System.Drawing.Size(55, 23);
this.loginButton.TabIndex = 2;
this.loginButton.Text = "Login";
this.loginButton.UseVisualStyleBackColor = true;
this.loginButton.Click += new System.EventHandler(this.loginButton_Click);
//
// listBox1
//
this.listBox1.ForeColor = System.Drawing.Color.Brown;
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(182, 21);
this.listBox1.MultiColumn = true;
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(139, 82);
this.listBox1.Sorted = true;
this.listBox1.TabIndex = 7;
//
// Form1
//
this.AcceptButton = this.loginButton;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.exitButton;
this.ClientSize = new System.Drawing.Size(327, 137);
this.ControlBox = false;
this.Controls.Add(this.listBox1);
this.Controls.Add(this.loginButton);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.exitButton);
this.Controls.Add(this.remButton);
this.Controls.Add(this.addButton);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = " User Administration Form";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button remButton;
private System.Windows.Forms.Button exitButton;
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.Button loginButton;
private System.Windows.Forms.ListBox listBox1;
}
}


Okie now let me start again......

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

gsvirdi
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]"

muster
1556

make another thread for this, it would be good

Feedback