关于登录的一些小记录
Login.aspx
<!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> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td align="center"> 用户名<asp:TextBox ID="txtName" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="center"> 密码<asp:TextBox ID="txtPwd" TextMode="Password" runat="server"></asp:TextBox> </td> </tr> <tr> <td align="center"> <asp:Button ID="btnLogin" runat="server" Text="Login" onclick="btnLogin_Click" /> </td> </tr> </table> </div> </form> </body> </html>
Login.aspx.cs
protected void btnLogin_Click(object sender, EventArgs e) { string userName = txtName.Text.Trim(), userPwd = txtPwd.Text.Trim(); //模拟数据库 Dictionary<string, User> dic = new Dictionary<string, User>(); User u = new User(); u.ID = 1; u.Name = "xupeihuagudulei"; u.Pwd = "xupeihuagudulei"; dic.Add("xupeihuagudulei", u); u = new User(); u.ID = 2; u.Name = "xupeihuagudulei1"; u.Pwd = "xupeihuagudulei1"; dic.Add("xupeihuagudulei1", u); u = new User(); u.ID = 3; u.Name = "temp"; u.Pwd = "123"; dic.Add("temp", u); if (dic.Keys.Contains(userName) && dic[userName].Pwd == userPwd) { Session["userID"] = dic[userName].ID; Response.Redirect("Main.aspx"); } else { ClientScript.RegisterStartupScript(GetType(), "error", "alert('用户名或密码错误!');", true); } } public class User //User 是与 Login.aspx.cs 同级的Class { public int ID { set; get; } public string Name { set; get; } public string Pwd { set; get; } }
PageBase 在App_Code 中 或类库中
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for PageBase /// </summary> public class PageBase : System.Web.UI.Page { public PageBase() { // // TODO: Add constructor logic here // } protected override void OnInit(EventArgs e) { //MustLogin 功能 if (Session["userID"] == null) { Response.Write("<script>alert('请先登录!');location.href='Login.aspx';</script>"); Response.End(); //ClientScript.RegisterStartupScript(GetType(), "error", "alert('请先登录!');location.href='Login.aspx';", true); } base.OnInit(e); } }
Main.aspx
<!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> </head> <body> <form id="form1" runat="server"> <table width='99%' height="100%" border='0' cellspacing='0' cellpadding='0'> <tr> <td style='padding-left: 3px; padding-top: 8px' valign="top"> <asp:PlaceHolder ID="phKJ" runat="server" Visible="false"> <!-- Item 2 Strat --> <dl class='bitem'> <dt onclick='showHide("items2_1")'><b>科技动态管理</b></dt> <dd style='display: block' class='sitem' id='items2_1'> <ul class='sitemu'> <li><a href='aspx/TechWatchAdd.aspx' target='main'>科技动态添加</a></li> <li><a href='aspx/TechWatchManage.aspx' target='main'>科技动态管理</a></li> </ul> </dd> </dl> <!-- Item 2 End --> </asp:PlaceHolder> <asp:PlaceHolder ID="phZC" runat="server" Visible="false"> <!-- Item 1 Strat --> <dl class='bitem'> <dt onclick='showHide("items1_1")'><b>政策法规管理</b></dt> <dd style='display: block' class='sitem' id='items1_1'> <ul class='sitemu'> <li><a href='aspx/RegulationAdd.aspx' target='main'>政策法规添加</a></li> <li><a href='aspx/RegulationManage.aspx' target='main'>政策法规管理</a> </li> </ul> </dd> </dl> <!-- Item 1 End --> </asp:PlaceHolder> <asp:PlaceHolder ID="phCX" runat="server" Visible="false"> <!-- Item 3 Strat --> <dl class='bitem'> <dt onclick='showHide("items3_1")'><b>创新服务管理</b></dt> <dd style='display: block' class='sitem' id='items3_1'> <ul class='sitemu'> <li><a href='aspx/ScienseCorporationAdd.aspx' target='main'>科技企业添加</a></li> <li><a href='aspx/ScienseCorporationManage.aspx' target='main'>科技企业管理</a></li> <li><a href='aspx/InnovationPlatformAdd.aspx' target='main'>创新平台添加</a></li> <li><a href='aspx/InnovationPlatformManage.aspx' target='main'>创新平台管理</a></li> <li><a href='aspx/Technology_achievementAdd.aspx' target='main'>科技成果添加</a></li> <li><a href='aspx/Technology_achievementManage.aspx' target='main'>科技成果管理</a></li> <li><a href='aspx/TechnologyMediatedAdd.aspx' target='main'>科技中介添加</a></li> <li><a href='aspx/TechnologyMediatedManage.aspx' target='main'>科技中介管理</a></li> </ul> </dd> </dl> <!-- Item 3 End --> </asp:PlaceHolder> <asp:PlaceHolder ID="phZS" runat="server" Visible="false"> <!-- Item 4 Strat --> <dl class='bitem'> <dt onclick='showHide("items4_1")'><b>知识产权管理</b></dt> <dd style='display: block' class='sitem' id='items4_1'> <ul class='sitemu'> <li><a href='aspx/PatentLawAdd.aspx' target='main'>专利法规添加</a></li> <li><a href='aspx/PatentLawManage.aspx' target='main'>专利法规管理</a></li> <li><a href='aspx/PatentDeclarateAdd.aspx' target='main'>专利申报添加</a></li> <li><a href='aspx/PatentDeclarateManage.aspx' target='main'>专利申报管理</a></li> <li><a href='archives.html' target='main'>专利咨询管理</a></li> </ul> </dd> </dl> <!-- Item 4 End --> </asp:PlaceHolder> <asp:PlaceHolder ID="phWS" runat="server" Visible="false"> <!-- Item 5 Strat --> <dl class='bitem'> <dt onclick='showHide("items5_1")'><b>网上办事管理</b></dt> <dd style='display: block' class='sitem' id='items5_1'> <ul class='sitemu'> <li><a href='aspx/DeclareGuideAdd.aspx' target='main'>申报指南添加</a></li> <li><a href='aspx/DeclareGuideManage.aspx' target='main'>申报指南管理</a></li> <li><a href='aspx/DownloadformAdd.aspx' target='main'>表格下载添加</a></li> <li><a href='aspx/DownloadformManage.aspx' target='main'>表格下载管理</a></li> <li><a href='archives.html' target='main'>网上公示添加</a></li> <li><a href='archives.html' target='main'>网上公示管理</a></li> </ul> </dd> </dl> <!-- Item 5 End --> </asp:PlaceHolder> </td> </tr> </table> </form> </body> </html>
Main.aspx.cs
protected void Page_Load(object sender, EventArgs e) { object userID = Session["userID"] ?? ""; int ID; if (int.TryParse(userID.ToString(), out ID)) { //用Dictionary<int, UserPurview> 模拟数据库存储 Dictionary<int, UserPurview> dic = new Dictionary<int, UserPurview>(); UserPurview up = new UserPurview(); up.UserID = 1; up.CX = true; up.KJ = true; up.WS = true; up.ZC = true; up.ZS = true; dic.Add(up.UserID, up); up = new UserPurview(); up.UserID = 2; up.CX = true; up.KJ = true; up.WS = false; up.ZC = false; up.ZS = true; dic.Add(up.UserID, up); up = new UserPurview(); up.UserID = 3; up.CX = true; up.KJ = false; up.WS = true; up.ZC = false; up.ZS = false; dic.Add(up.UserID, up); UserPurview currentUP; //模拟数据库查询 foreach (KeyValuePair<int, UserPurview> kvp in dic) { //找到当前用户 if (kvp.Key == ID) { currentUP = kvp.Value; phCX.Visible = currentUP.CX; phKJ.Visible = currentUP.KJ; phWS.Visible = currentUP.WS; phZC.Visible = currentUP.ZC; phZS.Visible = currentUP.ZS; } } } else { ClientScript.RegisterStartupScript(GetType(), "error", "alert('请先登录!');location.href='Login.aspx';", true); } } public class UserPurview { public int UserID { set; get; } public bool CX { set; get; } public bool KJ { set; get; } public bool WS { set; get; } public bool ZC { set; get; } public bool ZS { set; get; } }