[Solved] How to make login page in asp.net? [closed]


Well, before I begin let’s talk about the assumptions I’m going to have to make because you provided almost no information in your question. Bear in mind I have to make these assumptions so I can provide some kind of answer.

Database Schema

I’m going to assume the database schema looks something like this:

... TABLE users (
    userid INT PRIMARY KEY IDENTITY (1, 1),
    username VARCHAR(50),
    password VARCHAR(50)
)

Web Form

I’m going to assume the web form might look something like this:

<asp:TextBox ID="username" runat="server" />
<asp:TextBox ID="password" runat="server" />
<asp:Button ID="login" Text="Login" runat="server" />

Alright, so now that I’ve made some assumptions, here is how you would do it with the assumptions I’ve made. First we would hookup the Click event:

<asp:Button ID="login" Text="Login" runat="server" OnClick="LoginUser" />

and then we need the event handler in the code behind:

public void LoginUser(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection(connectionString);
    cn.Open();

    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM users WHERE username = @username AND password = @password", cn);

    using (cn)
    {
        sda.SelectCommand.Parameters.AddWithValue("@username", this.username.Text);
        sda.SelectCommand.Parameters.AddWithValue("@password", this.password.Text);

        sda.Fill(dt);

        if (dt.Rows.Count == 0)
        {
            // do something here, like show a label stating that the username
            // and password combination didn't match

            return;
        }

        // if you get here then it was a valid login
    }
}

Security

I made one more assumption in my example, for the sake of simplicity and brevity, I didn’t have an encrypted password in the database. You should. But you can add to this example as you get that into place, if you don’t already.

2

solved How to make login page in asp.net? [closed]