Click here to Skip to main content
15,886,554 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,


How to access username and password from text file using C#.??
Posted
Comments
kishore Rajendran 27-Mar-12 1:03am    
please explain more.
Bandu Baragali 27-Mar-12 1:10am    
Thank you for reply kishore
Bandu Baragali 27-Mar-12 1:10am    
for example:
file name test.txt
1.In this text file i will write like username = example and password = example1
2.when i click on login button it should verify with text file content. and display message like if it contains same username and password say login successfully. otherwise enter valid username and password.

The following methods can be used to write all the lines of text to text file and to read all the lines of text from text file.
C#
System.IO.File.WriteAllLines(fileName,new string[]{"username","password"});
string[] userData = System.IO.File.ReadAllLines(fileName);

Here, I have not used username =, password =. It can be used, then you have to extract the relevant portion from the string.

But, the major point is, if the username and in particular password is written to the text file as it is, any body can easily access and the whole purpose of authentication is defeated.
This aspect is explained by SAKryukov here
verify user name and password in c# form[^]
 
Share this answer
 
v5
Comments
Espen Harlinn 27-Mar-12 9:23am    
Nice reply :-D
ProEnggSoft 27-Mar-12 12:11pm    
Thank you very much.
This is not a good way to store username and password in textfile. anyway you can do like following


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string username, password;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        string filename = @"C:\Users\user\Desktop\Projects\CodeProject\textboxread\credentials.txt";
        // The textfile contains details
        //username=loginname
        //password=loginpassword

        var lines = File.ReadAllLines(filename);
        foreach (string line in lines)
        {
            if (line.Contains(@"username"))
            {
                string str = line; int index = str.IndexOf("="); 
                string newstr = str.Substring(index + 1);
                username = newstr.Trim();
            }
            if (line.Contains(@"password"))
            {
                string str = line; int index = str.IndexOf("="); 
                string newstr = str.Substring(index + 1);
                password = newstr.Trim();
            }

        }
        if((TextBox1.Text.Trim().Equals(username)) &&               (TextBox2.Text.Trim().Equals(password)))
        {
            //Login Code
        }
        else
        {
            Label1.Text = "Invalid Login";
        }

    }
}


 
Share this answer
 
v2
Comments
Bandu Baragali 27-Mar-12 1:44am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900