我是 C# ASP.NET 初学者,所以请原谅任何……不太正确的事情!
简而言之,我想创建一个非常基本的登录系统:一个通过数据库运行并使用会话的系统,这样只有登录的用户才能访问某些页面。我知道如何做大部分事情,但我坚持在登录页面上使用 LINQ 查询数据。
在登录页面上,我有一个用于选择用户名的 DropDownList、一个用于输入密码的文本框和一个用于登录的按钮(我还有一个用于错误的文字)。DropDownList 数据绑定到名为 DT_Test 的数据表。DT_Test 包含三列:UsernameID (int)、Username (nchar(30)) 和 Password (nchar(30))。UsernameID 是主键。
我想使用 DropDownList 和文本框从数据库中查询按钮的单击事件数据,以检查用户名和密码是否匹配。但是我不知道该怎么做...
当前代码(不是很多!):
前端:
<%@ 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>
后端:
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())
{
}
}
}
}
我花了几个小时搜索和尝试不同的代码,但似乎没有一个适合这种情况。
无论如何,感谢您的帮助和提示,非常感谢!
- 我知道安全风险等,但这不是一个实时网站或任何东西,它只是作为初学者的测试目的。*
更新代码:
后端:
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.";
}
}
}
}
}
前端:
<%@ 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>
- 我检查了数据库方面,就主键、列、实际数据和数据类而言,一切都很好。
与文本框相同:
后端:
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.";
}
}
}
前端:
<%@ 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>