query database and log in if username and password exist

Have database added into web forms project. Database Table named Users consist 3 columns. Id, Username, Password. Data in database is manually inserted. If user on login page inserts correct user and password, it should be redirected on another page.

My c# code :

using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"))
            {
                SqlCommand cmd = new SqlCommand("select * from Users;");
                cmd.Connection = con;
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                con.Close();
            }

How to get data from database and check if user put correct username and password to log in ?

Answers


Ideally the password shouldn't be stored as plaintext (it should be salted and hashed instead).

But if it is plaintext, it'd be a case of:

    using(SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"))
    {
        SqlCommand cmd = new SqlCommand("select * from Users where username like @username and password = @password;");
        cmd.Parameters.AddWithValue("@username", username);
        cmd.Parameters.AddWithValue("@password", password);
        cmd.Connection = con;
        con.Open();

        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        con.Close();

        bool loginSuccessful = ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0));

        if (loginSuccessful)
        {
            Console.WriteLine("Success!");
        } else {
            Console.WriteLine("Invalid username or password");
        }
    }

your method should something like this

string Command = "SELECT Id FROM User WHERE Username = @Username AND Password = @Password;";
using (SqlConnection myConnection = new SqlConnection(ConnectionString))
{
    myConnection.Open();
    using (SqlCommand myCommand = new SqlCommand(Command, myConnection))
    {
        myCommand.Parameters.Add("@Username", tbUser.Text);
        myCommand.Parameters.Add("@Password", tbPass.Text);
        return myCommand.ExecuteScalar() != null;
    }
}

Need Your Help

How to change the font family of Highchart to Bootstrap css default font family

css twitter-bootstrap fonts highcharts

I want to change the font of title, data label and tooltip to Bootstrap default default font in the pie chart (I'm using Highchart library to show the chart). I'm How can I change these? Tried to add

Android Camera unlock mediarecorder

android camera mediarecorder unlock

i want to write an app which records a video through the smartphone camera. i found this website for help : http://developer.android.com/guide/topics/media/camera.html#custom-camera

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.