Using LINQ to create a simple login

Posted by JDWebs on Stack Overflow See other posts from Stack Overflow or by JDWebs
Published on 2012-07-07T19:30:36Z Indexed on 2012/07/07 21:16 UTC
Read the original article Hit count: 230

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about database