首先做一个Session验证父类,需要验证的aspx页面继承该类

 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

 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


    <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">
                            
&nbsp;<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal></td>
                    
</tr>
                    
<tr>
                        
<td align="right" colspan="2" style="color: red; height: 17px">
                            
&nbsp;<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" />
                            
&nbsp;&nbsp;
                        
</td>
                    
</tr>
                    
<tr>
                        
<td align="right" colspan="2" style="height: 16px">
                            
&nbsp;</td>
                    
</tr>
                
</table>
            
</LayoutTemplate>
        
</asp:Login>
    
    
</div>
    
</form>
</body>
</html>

 

存储帐号信息的 xml 文件


  <person>
    
<name>admin</name>
    
<password>admin</password>
  
</person>
  
<person>
    
<name>google</name>
    
<password>google</password>
  
</person>
</user

 其它页面.cs

 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;
            }
        }
    }

相关文章:

  • 2022-12-23
  • 2021-08-08
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2019-03-12
  • 2021-06-25
  • 2021-10-21
猜你喜欢
  • 2022-03-07
  • 2022-12-23
  • 2022-02-26
  • 2022-12-23
  • 2021-11-06
  • 2021-09-01
相关资源
相似解决方案