Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="WebApplication2.Login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Login.aspx</title>
</head>
<body>
<form id="form1" runat="server">
<div>
username: <asp:TextBox ID="username" runat="server"></asp:TextBox>
<br />
passwd: <asp:TextBox ID="userpasswd" runat="server" ></asp:TextBox>
<br />
<asp:CheckBox ID="cb1" runat="server" Text="Rember me"/>
<%--<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="1">male</asp:ListItem>
<asp:ListItem Value="female">2</asp:ListItem>
</asp:DropDownList>--%>
<br />
<asp:Button ID="btlogin" runat="server" Text="login" OnClick="btlogin_Click" />
</div>
</form>
</body>
</html>
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebApplication2
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] != null && Session["userpwd"] != null)
{
Response.Redirect("Index.aspx");
}
else if(Request.Cookies["Name"] != null && Request.Cookies["Passwd"] != null)
{
Session["username"] = Request.Cookies["Name"].Value;
Session["userpwd"] = Request.Cookies["Passwd"].Value;
Response.Redirect("Index.aspx");
}
else
{
Response.Write("You haven't logged in to this website or (The Session or The Cookies has expired.)");
}
if (!IsPostBack)
{
Session["rember"] = false;
}
}
protected void btlogin_Click(object sender, EventArgs e)
{
if (username.Text != null && userpasswd != null)
{
string name = username.Text;
string passwd = userpasswd.Text;
bool CanLogin = false;
{
string strConn = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select count(*) from T_USER where UName = '" + name + "'";
cmd.CommandType = CommandType.Text;
if (Convert.ToInt32(cmd.ExecuteScalar()) == 0)
{
ClientScript.RegisterStartupScript(
this.GetType(),
"Alert",
"<script> alert(\"用户名不存在\")</script >"
);
}
else {
cmd.CommandText = "select count(*) from T_USER where UName = '" + name + "' and " + " UPwd = '" + passwd + "'";
cmd.CommandType = CommandType.Text;
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result == 0)
{
ClientScript.RegisterStartupScript(
this.GetType(),
"Alert",
"<script> alert(\" 用户名或密码不正确\")</script >"
);
}
else
{
CanLogin = true;
}
}
}
catch(SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
if (CanLogin == true)
{
Session["username"] = name;
Session["userpwd"] = passwd;
if (cb1.Checked)
{
int Days = 7;
Response.Cookies["Name"].Value = username.Text;
Response.Cookies["Passwd"].Value = userpasswd.Text;
Response.Cookies["Name"].Expires = DateTime.Now.AddDays(Days);
Response.Cookies["Passwd"].Expires = DateTime.Now.AddDays(Days);
}
Response.Redirect("Index.aspx");
}
else
{
Response.Write("用户名或密码输入不正确!");
}
}
else
{
Response.Write("用户名或密码输入不完全!");
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Index.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="WebApplication2.Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Index</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>这是index页面!</h1>
<br />
<p>
username: <% if(Session["username"] != null)
Response.Write(Session["username"].ToString());
%>
<br />
userpasswd: <% if(Session["userpwd"] != null)
Response.Write(Session["userpwd"].ToString());
%>
</p>
</div>
<asp:Button ID="btExit" runat="server" Text="Exit" OnClick="btExit_Click" />
</form>
</body>
</html>
Index.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
bool Logged = false;
if (Session["username"] != null && Session["userpwd"] != null)
{
Response.Write("Session Exist."+"<br />");
Logged = true;
}
if (Request.Cookies["Name"] != null && Request.Cookies["Passwd"] != null)
{
Response.Write("Cookies Exist." + "<br />");
Session["username"] = Request.Cookies["Name"].Value;
Session["userpwd"] = Request.Cookies["Passwd"].Value;
Logged = true;
}
if(Logged == false)
{
Response.Write("<script>alert("+"\"请先登录用户\""+");</script>");
Response.Redirect("Login.aspx");
}
}
protected void btExit_Click(object sender, EventArgs e)
{
Session.Remove("username");
Session.Remove("userpwd");
Response.Cookies["Name"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies["Passwd"].Expires = DateTime.Now.AddDays(-1);
Response.Redirect("Login.aspx");
}
}
}