从零开始编写自己的C#框架(15)——Web层后端登陆功能
对于一个后端管理系统,最重要内容之一的就是登陆页了,无论是安全验证、用户在线记录、相关日志记录、单用户或多用户使用帐号控制等,都是在这个页面进行处理的。
1、在解决方案中创建一个Web项目,并将它设置为启动项
2、添加引用
3、添加WebManage文件夹与Login.aspx文件
4、添加登陆页面HTML代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Solution.Web.Managers.WebManage.Login" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head id="Head1" runat="server">
6 <title>从零开始编写自己的C#框架——后端管理系统</title>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8 <link rel="stylesheet" href="Css/login.css" />
9 <script type="text/javascript">
10 function Random(n) { return (Math.floor(Math.random() * n)); };
11
12 function AjaxRnd() { return new Date().getTime() + '' + Random(10000); };
13
14 function ShowKey() {
15 document.getElementById("img_verifycode").src = "Base/Vcode.ashx?a=" + AjaxRnd();
16 };
17 </script>
18 </head>
19 <body>
20 <!--CENTER开始-->
21 <div class="login-container">
22 <form id="form1" runat="server">
23 <div class="login-header">
24 <h3>
25 Login</h3>
26 </div>
27 <div id="login-content" class="clearfix">
28 <div>
29 <label>
30 用户名</label>
31 <div>
32 <asp:TextBox runat="server" ID="txtusername" CssClass="input w92" />
33 </div>
34 <label>
35 密码</label>
36 <div>
37 <asp:TextBox runat="server" ID="txtpass" CssClass="input w92" TextMode="Password" />
38 </div>
39 <label>
40 验证码</label>
41 <div>
42 <asp:TextBox runat="server" ID="txtcode" CssClass="input w100 fl" />
43 <asp:Image ID="img_verifycode" runat="server" onclick="ShowKey();" ToolTip="更换验证码"
44 ImageUrl="Base/Vcode.ashx" />
45 <div class="fc"></div>
46 </div>
47 </div>
48 <div>
49 <asp:Button ID="BtnLogin" CssClass="btn" runat="server" OnClick="BtnLogin_Click"
50 Text="登陆" />
51 </div>
52 </div>
53 </form>
54 </div>
55 <!--CENTER结束-->
56 </body>
57 </html>
css样式在本文后面的解决方案中有
浏览一下效果
5、后端管理系统登陆验证流程图
我们根据下面的流程图来编写登陆页代码
6、添加在线列表数据表并修改管事员表相关字段
执行下面SQL语句
1 /****** Object: Table [dbo].[Manager] Script Date: 2014/6/4 22:27:52 ******/
2 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Manager]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
3 drop table [dbo].[Manager]
4 GO
5
6 /****** Object: Table [dbo].[Manager] Script Date: 2014/6/4 22:27:52 ******/
7 CREATE TABLE [dbo].[Manager] (
8 [Id] [int] IDENTITY (1, 1) NOT NULL,
9 [LoginName] [nvarchar] (20) NOT NULL,
10 [LoginPass] [nvarchar] (32) NOT NULL,
11 [LoginTime] [datetime] NOT NULL,
12 [LoginIp] [nvarchar] (30) NOT NULL,
13 [LoginCount] [int] NOT NULL,
14 [CreateTime] [datetime] NOT NULL,
15 [UpdateTime] [datetime] NOT NULL,
16 [IsMultiUser] [tinyint] NOT NULL,
17 [Branch_Id] [int] NOT NULL,
18 [Branch_Code] [nvarchar] (20) NOT NULL,
19 [Branch_Name] [nvarchar] (25) NOT NULL,
20 [Position_Id] [nvarchar] (100) NOT NULL,
21 [Position_Name] [nvarchar] (30) NOT NULL,
22 [IsWork] [tinyint] NOT NULL,
23 [IsEnable] [tinyint] NOT NULL,
24 [CName] [nvarchar] (20) NOT NULL,
25 [EName] [nvarchar] (50) NOT NULL,
26 [PhotoImg] [nvarchar] (250) NOT NULL,
27 [Sex] [nvarchar] (4) NOT NULL,
28 [Birthday] [nvarchar] (20) NOT NULL,
29 [NativePlace] [nvarchar] (100) NOT NULL,
30 [NationalName] [nvarchar] (50) NOT NULL,
31 [Record] [nvarchar] (25) NOT NULL,
32 [GraduateCollege] [nvarchar] (30) NOT NULL,
33 [GraduateSpecialty] [nvarchar] (50) NOT NULL,
34 [Tel] [nvarchar] (30) NOT NULL,
35 [Mobile] [nvarchar] (30) NOT NULL,
36 [Email] [nvarchar] (50) NOT NULL,
37 [Qq] [nvarchar] (30) NOT NULL,
38 [Msn] [nvarchar] (30) NOT NULL,
39 [Address] [nvarchar] (100) NOT NULL,
40 [Content] [ntext] NOT NULL,
41 [Manager_Id] [int] NOT NULL,
42 [Manager_CName] [nvarchar] (20) NOT NULL
43 ) ON [PRIMARY]
44 GO
45
46 ALTER TABLE [dbo].[Manager] WITH NOCHECK ADD
47 CONSTRAINT [PK_Manager] PRIMARY KEY CLUSTERED
48 (
49 [Id]
50 ) ON [PRIMARY]
51 GO
52
53 ALTER TABLE [dbo].[Manager] ADD
54 CONSTRAINT [DF_Manager_LoginName] DEFAULT ('') FOR [LoginName],
55 CONSTRAINT [DF_Manager_LoginPass] DEFAULT ('') FOR [LoginPass],
56 CONSTRAINT [DF_Manager_LoginTime] DEFAULT (getdate()) FOR [LoginTime],
57 CONSTRAINT [DF_Manager_LoginIp] DEFAULT ('') FOR [LoginIp],
58 CONSTRAINT [DF_Manager_LoginCount] DEFAULT (0) FOR [LoginCount],
59 CONSTRAINT [DF_Manager_CreateTime] DEFAULT (getdate()) FOR [CreateTime],
60 CONSTRAINT [DF_Manager_UpdateTime] DEFAULT (getdate()) FOR [UpdateTime],
61 CONSTRAINT [DF_Manager_IsMultiUser] DEFAULT (0) FOR [IsMultiUser],
62 CONSTRAINT [DF_Manager_Branch_Id] DEFAULT (0) FOR [Branch_Id],
63 CONSTRAINT [DF_Manager_Branch_Code] DEFAULT ('') FOR [Branch_Code],
64 CONSTRAINT [DF_Manager_Branch_Name] DEFAULT ('') FOR [Branch_Name],
65 CONSTRAINT [DF_Manager_Position_Id] DEFAULT ('') FOR [Position_Id],
66 CONSTRAINT [DF_Manager_Position_Name] DEFAULT ('') FOR [Position_Name],
67 CONSTRAINT [DF_Manager_IsWork] DEFAULT (0) FOR [IsWork],
68 CONSTRAINT [DF_Manager_IsEnable] DEFAULT (1) FOR [IsEnable],
69 CONSTRAINT [DF_Manager_CName] DEFAULT ('') FOR [CName],
70 CONSTRAINT [DF_Manager_EName] DEFAULT ('') FOR [EName],
71 CONSTRAINT [DF_Manager_PhotoImg] DEFAULT ('') FOR [PhotoImg],
72 CONSTRAINT [DF_Manager_Sex] DEFAULT ('') FOR [Sex],
73 CONSTRAINT [DF_Manager_Birthday] DEFAULT ('') FOR [Birthday],
74 CONSTRAINT [DF_Manager_NativePlace] DEFAULT ('') FOR [NativePlace],
75 CONSTRAINT [DF_Manager_NationalName] DEFAULT ('') FOR [NationalName],
76 CONSTRAINT [DF_Manager_Record] DEFAULT ('') FOR [Record],
77 CONSTRAINT [DF_Manager_GraduateCollege] DEFAULT ('') FOR [GraduateCollege],
78 CONSTRAINT [DF_Manager_GraduateSpecialty] DEFAULT ('') FOR [GraduateSpecialty],
79 CONSTRAINT [DF_Manager_Tel] DEFAULT ('') FOR [Tel],
80 CONSTRAINT [DF_Manager_Mobile] DEFAULT ('') FOR [Mobile],
81 CONSTRAINT [DF_Manager_Email] DEFAULT ('') FOR [Email],
82 CONSTRAINT [DF_Manager_Qq] DEFAULT ('') FOR [Qq],
83 CONSTRAINT [DF_Manager_Msn] DEFAULT ('') FOR [Msn],
84 CONSTRAINT [DF_Manager_Address] DEFAULT ('') FOR [Address],
85 CONSTRAINT [DF_Manager_Content] DEFAULT ('') FOR [Content],
86 CONSTRAINT [DF_Manager_Manager_Id] DEFAULT (0