首先做一个Session验证父类,需要验证的aspx页面继承该类
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// IdentifyCheck 的摘要说明
/// </summary>
public class IdentifyCheck:Page
{
public IdentifyCheck()
{
}
protected override void OnLoad(EventArgs e)
{
if (Session["UserName"] == null || Session["UserName"].ToString().Trim().ToLower() != "admin")
{
//Response.Redirect(ResolveUrl("~/Manager/login.aspx"));
Response.Redirect(ResolveUrl("~/manager/login.aspx"));
}
}
}
登陆页面 login.aspx.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class Manager_login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["UserName"] = null;
Session["PassWord"] = null;
}
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
if (IdentifyCheck(this.Login1.UserName, this.Login1.Password))
{
Session["UserName"] = this.Login1.UserName;
Session["PassWord"] = this.Login1.Password;
this.Response.Redirect("default.aspx");
}
}
bool IdentifyCheck(string strName, string strPass)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("user.xml"));
XmlNode root = xmlDoc.SelectSingleNode("user");
XmlNodeList childList = root.ChildNodes;
foreach (XmlNode node in childList)
{
XmlNodeList childPersonLst = node.ChildNodes;
if (childPersonLst[0].InnerText.ToString() == this.Login1.UserName &&
childPersonLst[1].InnerText.ToString() == this.Login1.Password)
{
return true;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
return false;
}
}
login.aspx
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="Manager_login" %>
<!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>管理员登录</title>
<style type="text/css">
body{text-align:center;}
#style{
margin-left:auto;
margin-right:auto;
margin-top:120px;}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="style">
<asp:Login ID="Login1" runat="server" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em"
ForeColor="Red" Height="40px" OnAuthenticate="Login1_Authenticate">
<TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="0.9em" ForeColor="White" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284775" />
<LayoutTemplate>
<table border="0" cellpadding="0" style="height: 100px">
<tr>
<td align="center" colspan="2" style="font-weight: bold; font-size: 0.9em; color: white;
background-color: #5d7b9d">
登录</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" ForeColor="Black">用户名:</asp:Label></td>
<td>
<asp:TextBox ID="UserName" runat="server" Font-Size="0.8em"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" ForeColor="Black">密码:</asp:Label></td>
<td>
<asp:TextBox ID="Password" runat="server" Font-Size="0.8em" TextMode="Password" Width="102px"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。" ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></td>
</tr>
<tr>
<td align="right" colspan="2" style="color: red; height: 17px">
<asp:Button ID="LoginButton" runat="server" BackColor="#FFFBFF" BorderColor="#CCCCCC"
BorderStyle="Solid" BorderWidth="1px" CommandName="Login" Font-Names="Verdana"
Font-Size="0.8em" ForeColor="#284775" Text="登录" ValidationGroup="Login1" />
</td>
</tr>
<tr>
<td align="right" colspan="2" style="height: 16px">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
</div>
</form>
</body>
</html>
存储帐号信息的 xml 文件
Code
<?xml version="1.0" encoding="utf-8"?>
<user>
<person>
<name>admin</name>
<password>admin</password>
</person>
<person>
<name>google</name>
<password>google</password>
</person>
</user
其它页面.cs
Code
public partial class Manager_ModifyNews : IdentifyCheck
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e); //调用base's Onload进行验证
if (!IsPostBack)
{
#region Data Bind
this.DDownClass.DataSource = NewsDAL.getAllClass();
this.DDownClass.DataTextField = "ClassName";
this.DDownClass.DataValueField = "iClassID";
this.DDownClass.DataBind();
#endregion
if (Request.QueryString["iNewsID"] != null && Request.QueryString["iNewsID"].ToString().Trim() != "")
{
NewsEntity Instance = NewsDAL.getNewsByID(Convert.ToInt32(Request.QueryString["iNewsID"]));
this.TBoxTitle.Text = Instance.sNewsTitle != null ? Instance.sNewsTitle : "";
this.TBoxAuthor.Text = Instance.sAuthor != null ? Instance.sAuthor : "";
this.HiddenField1.Value = Instance.sNewsBody != null ? Instance.sNewsBody : "";
ViewState["iNewsID"] = Instance.iNewsID;
}
else
{
return;
}
}
}