I’m an C# ASP.NET beginner, so please excuse anything that’s… not quite right!
In short, I want to create a really basic login system: one which runs through a database and uses sessions so that only logged in users can access certain pages. I know how to do most of that, but I’m stuck with querying data with LINQ on the login page.
On the login page, I have a DropDownList to select a username, a Textbox to type in a password and a button to login (I also have a literal for errors). The DropDownList is databound to a datatable called DT_Test. DT_Test contains three columns: UsernameID (int), Username (nchar(30)) and Password (nchar(30)). UsernameID is the primary key.
I want to make the button’s click event query data from the database with the DropDownList and Textbox, in order to check if the username and password match. But I don’t know how to do this…
Current Code (not a lot!):
Front End:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_Test.aspx.cs" Inherits="Login_Login_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Test</title>
</head>
<body>
<form id="LoginTest" runat="server">
<div>
<asp:DropDownList ID="DDL_Username" runat="server" Height="20px"
DataTextField="txt">
</asp:DropDownList>
<br />
<asp:TextBox ID="TB_Password" runat="server" TextMode="Password"></asp:TextBox>
<br />
<asp:Button ID="B_Login" runat="server" onclick="B_Login_Click" Text="Login" />
<br />
<asp:Literal ID="LI_Result" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
Back End:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login_Login_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Binder();
}
}
private void Binder()
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
DDL_Username.DataSource = from x in db.DT_Honeys select new { x.UsernameID, txt = x.Username };
DDL_Username.DataBind();
}
}
protected void B_Login_Click(object sender, EventArgs e)
{
if (TB_Password.Text != "")
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
}
}
}
}
I have spent hours searching and trying different code, but none of it seems to fit in for this context.
Anyway, help and tips appreciated, thank you very much!
- I am aware of security risks etc. but this is not a live website or anything, it is simply for testing purposes as a beginner. *
Updated code:
Back End:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login_Page_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Tester();
}
}
private void Tester()
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
DDL_Username.DataSource = from x in db.DT_Honeys select new { id = x.UsernameID, txt = x.Username };
DDL_Username.DataValueField = "id";
DDL_Username.DataTextField = "txt";
DDL_Username.DataBind();
}
}
protected void B_Login_Click(object sender, EventArgs e)
{
if (TB_Password.Text != "")
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
DT_Honey blah = new DT_Honey();
blah = db.DT_Honeys.SingleOrDefault(x => x.UsernameID == int.Parse(DDL_Username.SelectedValue.ToString()));
if (blah != null)
{
if (TB_Password.Text.ToString().Trim() == blah.Password.ToString())
{
LI_Result.Text = "Credentials correct.";
}
else
{
LI_Result.Text = "Error: credentials are incorrect.";
}
}
else
{
LI_Result.Text = "Error: null value.";
}
}
}
}
}
Front End:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_Page_Test.aspx.cs" Inherits="Login_Page_Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Logi Page Test</title>
</head>
<body>
<form id="LoginPageTest" runat="server">
<div>
</div>
<asp:DropDownList ID="DDL_Username" runat="server">
</asp:DropDownList>
<br />
<asp:TextBox ID="TB_Password" runat="server"></asp:TextBox>
<br />
<asp:Button ID="B_Login" runat="server" onclick="B_Login_Click" Text="Login" />
<br />
<asp:Literal ID="LI_Result" runat="server"></asp:Literal>
</form>
</body>
</html>
- I have checked the database aspects, and it is all fine in terms of primary key, columns, actual data, and dataclasses.
Same with textbox:
Back End:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login_Login_Page_2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void B_Login_Click(object sender, EventArgs e)
{
if (TB_Username.Text.ToString().Trim() != "" && TB_Password.Text.ToString().Trim() != "")
{
using (DataClassesDataContext db = new DataClassesDataContext())
{
DT_Honey Login = new DT_Honey();
Login = db.DT_Honeys.SingleOrDefault(y => y.UsernameID == int.Parse(TB_Username.Text.ToString().Trim()));
if (Login != null)
{
if (TB_Password.Text.Trim() == Login.Password.ToString().Trim())
{
LI_Result.Text = "Yeah! The credentials you entered were correct!";
}
}
else
{
LI_Result.Text = "Oops! There was an error with the credentials you entered; please try again.";
}
}
}
else
{
LI_Result.Text = "Wow! Please fill out <b>both</b> the Username and Password text fields to login; thank you.";
}
}
}
Front End:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login_Page_2.aspx.cs" Inherits="Login_Login_Page_2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login Page 2</title>
</head>
<body>
<form id="LoginPage2" runat="server">
<div>
<asp:TextBox ID="TB_Username" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TB_Password" runat="server"></asp:TextBox>
<br />
<asp:Button ID="B_Login" runat="server" onclick="B_Login_Click" Text="Login" />
<br />
<asp:Literal ID="LI_Result" runat="server"></asp:Literal>
</div>
</form>
</body>
</html>
Maybe this will help you?:
However, best practice would be to use a standard membership provider for ASP.NET. Then you don’t have to be in the business of setting up your own membership schema in a database. (It looks as though you’re storing passwords in clear text, but a membership provider will fix that and handle the details for you.)
MSDN Introduction to Membership
Edit You’ll probably want to change the Binder code so that your drop down items’ IDs match up with the database ID numbers: