从零开始编写自己的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>
View Code

  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) FOR [Manager_Id],
 87   CONSTRAINT [DF_Manager_Manager_CName] DEFAULT ('') FOR [Manager_CName] 
 88 GO
 89 
 90 CREATE  INDEX [IX_Manager__LoginName] ON [dbo].[Manager]([LoginName]) ON [PRIMARY]
 91 GO
 92 
 93 CREATE  INDEX [IX_Manager__LoginTime] ON [dbo].[Manager]([LoginTime]) ON [PRIMARY]
 94 GO
 95 
 96 CREATE  INDEX [IX_Manager__CreateTime] ON [dbo].[Manager]([CreateTime]) ON [PRIMARY]
 97 GO
 98 
 99 CREATE  INDEX [IX_Manager__UpdateTime] ON [dbo].[Manager]([UpdateTime]) ON [PRIMARY]
100 GO
101 
102 CREATE  INDEX [IX_Manager__Branch_Id] ON [dbo].[Manager]([Branch_Id]) ON [PRIMARY]
103 GO
104 
105 CREATE  INDEX [IX_Manager__Branch_Code] ON [dbo].[Manager]([Branch_Code]) ON [PRIMARY]
106 GO
107 
108 CREATE  INDEX [IX_Manager__Position_Id] ON [dbo].[Manager]([Position_Id]) ON [PRIMARY]
109 GO
110 
111 CREATE  INDEX [IX_Manager__IsWork] ON [dbo].[Manager]([IsWork]) ON [PRIMARY]
112 GO
113 
114 CREATE  INDEX [IX_Manager__IsEnable] ON [dbo].[Manager]([IsEnable]) ON [PRIMARY]
115 GO
116 
117 CREATE  INDEX [IX_Manager__CName] ON [dbo].[Manager]([CName]) ON [PRIMARY]
118 GO
119 
120 CREATE  INDEX [IX_Manager__EName] ON [dbo].[Manager]([EName]) ON [PRIMARY]
121 GO
122 
123 CREATE  INDEX [IX_Manager__Sex] ON [dbo].[Manager]([Sex]) ON [PRIMARY]
124 GO
125 
126 exec sp_addextendedproperty N'MS_Description', N'主键Id', N'user', N'dbo', N'table', N'Manager', N'column', N'Id'
127 GO
128 
129 exec sp_addextendedproperty N'MS_Description', N'登陆账号', N'user', N'dbo', N'table', N'Manager', N'column', N'LoginName'
130 GO
131 
132 exec sp_addextendedproperty N'MS_Description', N'登陆密码', N'user', N'dbo', N'table', N'Manager', N'column', N'LoginPass'
133 GO
134 
135 exec sp_addextendedproperty N'MS_Description', N'最后登陆时间', N'user', N'dbo', N'table', N'Manager', N'column', N'LoginTime'
136 GO
137 
138 exec sp_addextendedproperty N'MS_Description', N'最后登陆IP', N'user', N'dbo', N'table', N'Manager', N'column', N'LoginIp'
139 GO
140 
141 exec sp_addextendedproperty N'MS_Description', N'登陆次数', N'user', N'dbo', N'table', N'Manager', N'column', N'LoginCount'
142 GO
143 
144 exec sp_addextendedproperty N'MS_Description', N'注册时间', N'user', N'dbo', N'table', N'Manager', N'column', N'CreateTime'
145 GO
146 
147 exec sp_addextendedproperty N'MS_Description', N'资料最后修改日期', N'user', N'dbo', N'table', N'Manager', N'column', N'UpdateTime'
148 GO
149 
150 exec sp_addextendedproperty N'MS_Description', N'是否允许同一帐号多人使用,0=只能单个在线,1=可以多人同时在线', N'user', N'dbo', N'table', N'Manager', N'column', N'IsMultiUser'
151 GO
152 
153 exec sp_addextendedproperty N'MS_Description', N'所属部门ID', N'user', N'dbo', N'table', N'Manager', N'column', N'Branch_Id'
154 GO
155 
156 exec sp_addextendedproperty N'MS_Description', N'所属部门编号,用户只能正式归属于一个部门', N'user', N'dbo', N'table', N'Manager', N'column', N'Branch_Code'
157 GO
158 
159 exec sp_addextendedproperty N'MS_Description', N'部门名称', N'user', N'dbo', N'table', N'Manager', N'column', N'Branch_Name'
160 GO
161 
162 exec sp_addextendedproperty N'MS_Description', N'用户职位ID', N'user', N'dbo', N'table', N'Manager', N'column', N'Position_Id'
163 GO
164 
165 exec sp_addextendedproperty N'MS_Description', N'职位名称', N'user', N'dbo', N'table', N'Manager', N'column', N'Position_Name'
166 GO
167 
168 exec sp_addextendedproperty N'MS_Description', N'0=离职,1=就职', N'user', N'dbo', N'table', N'Manager', N'column', N'IsWork'
169 GO
170 
171 exec sp_addextendedproperty N'MS_Description', N'账号是否启用,1=true(启用),0=false(禁用)', N'user', N'dbo', N'table', N'Manager', N'column', N'IsEnable'
172 GO
173 
174 exec sp_addextendedproperty N'MS_Description', N'用户中文名称', N'user', N'dbo', N'table', N'Manager', N'column', N'CName'
175 GO
176 
177 exec sp_addextendedproperty N'MS_Description', N'用户英文名称', N'user', N'dbo', N'table', N'Manager', N'column', N'EName'
178 GO
179 
180 exec sp_addextendedproperty N'MS_Description', N'头像图片路径', N'user', N'dbo', N'table', N'Manager', N'column', N'PhotoImg'
181 GO
182 
183 exec sp_addextendedproperty N'MS_Description', N'性别(0=未知,1=男,2=女)', N'user', N'dbo', N'table', N'Manager', N'column', N'Sex'
184 GO
185 
186 exec sp_addextendedproperty N'MS_Description', N'出生日期', N'user', N'dbo', N'table', N'Manager', N'column', N'Birthday'
187 GO
188 
189 exec sp_addextendedproperty N'MS_Description', N'籍贯', N'user', N'dbo', N'table', N'Manager', N'column', N'NativePlace'
190 GO
191 
192 exec sp_addextendedproperty N'MS_Description', N'民族', N'user', N'dbo', N'table', N'Manager', N'column', N'NationalName'
193 GO
194 
195 exec sp_addextendedproperty N'MS_Description', N'个人--学历', N'user', N'dbo', N'table', N'Manager', N'column', N'Record'
196 GO
197 
198 exec sp_addextendedproperty N'MS_Description', N'毕业学校', N'user', N'dbo', N'table', N'Manager', N'column', N'GraduateCollege'
199 GO
200 
201 exec sp_addextendedproperty N'MS_Description', N'毕业专业', N'user', N'dbo', N'table', N'Manager', N'column', N'GraduateSpecialty'
202 GO
203 
204 exec sp_addextendedproperty N'MS_Description', N'个人--联系电话', N'user', N'dbo', N'table', N'Manager', N'column', N'Tel'
205 GO
206 
207 exec sp_addextendedproperty N'MS_Description', N'个人--移动电话', N'user', N'dbo', N'table', N'Manager', N'column', N'Mobile'
208 GO
209 
210 exec sp_addextendedproperty N'MS_Description', N'个人--联系邮箱', N'user', N'dbo', N'table', N'Manager', N'column', N'Email'
211 GO
212 
213 exec sp_addextendedproperty N'MS_Description', N'个人--QQ', N'user', N'dbo', N'table', N'Manager', N'column', N'Qq'
214 GO
215 
216 exec sp_addextendedproperty N'MS_Description', N'个人--Msn', N'user', N'dbo', N'table', N'Manager', N'column', N'Msn'
217 GO
218 
219 exec sp_addextendedproperty N'MS_Description', N'个人--通讯地址', N'user', N'dbo', N'table', N'Manager', N'column', N'Address'
220 GO
221 
222 exec sp_addextendedproperty N'MS_Description', N'备注', N'user', N'dbo', N'table', N'Manager', N'column', N'Content'
223 GO
224 
225 exec sp_addextendedproperty N'MS_Description', N'修改人员id', N'user', N'dbo', N'table', N'Manager', N'column', N'Manager_Id'
226 GO
227 
228 exec sp_addextendedproperty N'MS_Description', N'修改人中文名称', N'user', N'dbo', N'table', N'Manager', N'column', N'Manager_CName'
229 GO
230 
231 /****** Object:  Table [dbo].[OnlineUsers]    Script Date: 2014/6/4 22:27:52  ******/
232 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OnlineUsers]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
233 drop table [dbo].[OnlineUsers]
234 GO
235 
236 /****** Object:  Table [dbo].[OnlineUsers]    Script Date: 2014/6/4 22:27:52   ******/
237 CREATE TABLE [dbo].[OnlineUsers] (
238   [Id] [int] IDENTITY (1, 1) NOT NULL,
239   [UserHashKey] [nvarchar] (50) NOT NULL,
240   [Manager_Id] [int] NOT NULL,
241   [Manager_LoginName] [nvarchar] (20) NOT NULL,
242   [Manager_LoginPass] [nvarchar] (32) NOT NULL,
243   [Manager_CName] [nvarchar] (20) NOT NULL,
244   [LoginTime] [datetime] NOT NULL,
245   [LoginIp] [nvarchar] (30) NOT NULL,
246   [UserKey] [nvarchar] (32) NOT NULL,
247   [Md5] [nvarchar] (32) NOT NULL,
248   [UpdateTime] [datetime] NOT NULL,
249   [Sex] [nvarchar] (4) NOT NULL,
250   [Branch_Id] [int] NOT NULL,
251   [Branch_Code] [nvarchar] (20) NOT NULL,
252   [Branch_Name] [nvarchar] (25) NOT NULL,
253   [Position_Id] [nvarchar] (100) NOT NULL,
254   [Position_Name] [nvarchar] (30) NOT NULL,
255   [CurrentPage] [nvarchar] (100) NOT NULL,
256   [CurrentPageTitle] [nvarchar] (250) NOT NULL,
257   [SessionId] [nvarchar] (100) NOT NULL,
258   [UserAgent] [nvarchar] (1000) NOT NULL,
259   [OperatingSystem] [nvarchar] (50) NOT NULL,
260   [TerminalType] [int] NOT NULL,
261   [BrowserName] [nvarchar] (50) NOT NULL,
262   [BrowserVersion] [nvarchar] (10) NOT NULL
263 ) ON [PRIMARY]
264 GO
265 
266 ALTER TABLE [dbo].[OnlineUsers] WITH NOCHECK ADD
267   CONSTRAINT [PK_OnlineUsers] PRIMARY KEY  CLUSTERED
268 (
269  [Id]
270 )  ON [PRIMARY]
271 GO
272 
273 ALTER TABLE [dbo].[OnlineUsers] ADD
274   CONSTRAINT [DF_OnlineUsers_UserHashKey] DEFAULT ('') FOR [UserHashKey],
275   CONSTRAINT [DF_OnlineUsers_Manager_Id] DEFAULT (0) FOR [Manager_Id],
276   CONSTRAINT [DF_OnlineUsers_Manager_LoginName] DEFAULT ('') FOR [Manager_LoginName],
277   CONSTRAINT [DF_OnlineUsers_Manager_LoginPass] DEFAULT ('') FOR [Manager_LoginPass],
278   CONSTRAINT [DF_OnlineUsers_Manager_CName] DEFAULT ('') FOR [Manager_CName],
279   CONSTRAINT [DF_OnlineUsers_LoginTime] DEFAULT (getdate()) FOR [LoginTime],
280   CONSTRAINT [DF_OnlineUsers_LoginIp] DEFAULT ('') FOR [LoginIp],
281   CONSTRAINT [DF_OnlineUsers_UserKey] DEFAULT ('') FOR [UserKey],
282   CONSTRAINT [DF_OnlineUsers_Md5] DEFAULT ('') FOR [Md5],
283   CONSTRAINT [DF_OnlineUsers_UpdateTime] DEFAULT (getdate()) FOR [UpdateTime],
284   CONSTRAINT [DF_OnlineUsers_Sex] DEFAULT ('') FOR [Sex],
285   CONSTRAINT [DF_OnlineUsers_Branch_Id] DEFAULT (0) FOR [Branch_Id],
286   CONSTRAINT [DF_OnlineUsers_Branch_Code] DEFAULT ('') FOR [Branch_Code],
287   CONSTRAINT [DF_OnlineUsers_Branch_Name] DEFAULT ('') FOR [Branch_Name],
288   CONSTRAINT [DF_OnlineUsers_Position_Id] DEFAULT ('') FOR [Position_Id],
289   CONSTRAINT [DF_OnlineUsers_Position_Name] DEFAULT ('') FOR [Position_Name],
290   CONSTRAINT [DF_OnlineUsers_CurrentPage] DEFAULT ('') FOR [CurrentPage],
291   CONSTRAINT [DF_OnlineUsers_CurrentPageTitle] DEFAULT ('') FOR [CurrentPageTitle],
292   CONSTRAINT [DF_OnlineUsers_SessionId] DEFAULT ('') FOR [SessionId],
293   CONSTRAINT [DF_OnlineUsers_UserAgent] DEFAULT ('') FOR [UserAgent],
294   CONSTRAINT [DF_OnlineUsers_OperatingSystem] DEFAULT ('') FOR [OperatingSystem],
295   CONSTRAINT [DF_OnlineUsers_TerminalType] DEFAULT (0) FOR [TerminalType],
296   CONSTRAINT [DF_OnlineUsers_BrowserName] DEFAULT ('') FOR [BrowserName],
297   CONSTRAINT [DF_OnlineUsers_BrowserVersion] DEFAULT ('') FOR [BrowserVersion] 
298 GO
299 
300 CREATE  INDEX [IX_OnlineUsers__Manager_Id] ON [dbo].[OnlineUsers]([Manager_Id]) ON [PRIMARY]
301 GO
302 
303 CREATE  INDEX [IX_OnlineUsers__Manager_LoginName] ON [dbo].[OnlineUsers]([Manager_LoginName]) ON [PRIMARY]
304 GO
305 
306 CREATE  INDEX [IX_OnlineUsers__Manager_CName] ON [dbo].[OnlineUsers]([Manager_CName]) ON [PRIMARY]
307 GO
308 
309 CREATE  INDEX [IX_OnlineUsers__LoginTime] ON [dbo].[OnlineUsers]([LoginTime]) ON [PRIMARY]
310 GO
311 
312 CREATE  INDEX [IX_OnlineUsers__UpdateTime] ON [dbo].[OnlineUsers]([UpdateTime]) ON [PRIMARY]
313 GO
314 
315 CREATE  INDEX [IX_OnlineUsers__Branch_Id] ON [dbo].[OnlineUsers]([Branch_Id]) ON [PRIMARY]
316 GO
317 
318 CREATE  INDEX [IX_OnlineUsers__Branch_Code] ON [dbo].[OnlineUsers]([Branch_Code]) ON [PRIMARY]
319 GO
320 
321 CREATE  INDEX [IX_OnlineUsers__Position_Id] ON [dbo].[OnlineUsers]([Position_Id]) ON [PRIMARY]
322 GO
323 
324 exec sp_addextendedproperty N'MS_Description', N'主键Id', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Id'
325 GO
326 
327 exec sp_addextendedproperty N'MS_Description', N'在线用户列表中的HashTable Key值', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'UserHashKey'
328 GO
329 
330 exec sp_addextendedproperty N'MS_Description', N'用户Id', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Manager_Id'
331 GO
332 
333 exec sp_addextendedproperty N'MS_Description', N'登陆账号', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Manager_LoginName'
334 GO
335 
336 exec sp_addextendedproperty N'MS_Description', N'登陆密码', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Manager_LoginPass'
337 GO
338 
339 exec sp_addextendedproperty N'MS_Description', N'用户中文名称', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Manager_CName'
340 GO
341 
342 exec sp_addextendedproperty N'MS_Description', N'登陆时间', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'LoginTime'
343 GO
344 
345 exec sp_addextendedproperty N'MS_Description', N'登陆IP', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'LoginIp'
346 GO
347 
348 exec sp_addextendedproperty N'MS_Description', N'用户密钥', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'UserKey'
349 GO
350 
351 exec sp_addextendedproperty N'MS_Description', N'Md5(密钥+登陆帐号+密码+IP+密钥.Substring(6,8))', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Md5'
352 GO
353 
354 exec sp_addextendedproperty N'MS_Description', N'最后在线时间', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'UpdateTime'
355 GO
356 
357 exec sp_addextendedproperty N'MS_Description', N'性别(0=未知,1=男,2=女)', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Sex'
358 GO
359 
360 exec sp_addextendedproperty N'MS_Description', N'所属部门ID', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Branch_Id'
361 GO
362 
363 exec sp_addextendedproperty N'MS_Description', N'所属部门编号,用户只能正式归属于一个部门', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Branch_Code'
364 GO
365 
366 exec sp_addextendedproperty N'MS_Description', N'部门名称', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Branch_Name'
367 GO
368 
369 exec sp_addextendedproperty N'MS_Description', N'用户职位ID', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Position_Id'
370 GO
371 
372 exec sp_addextendedproperty N'MS_Description', N'职位名称', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'Position_Name'
373 GO
374 
375 exec sp_addextendedproperty N'MS_Description', N'用户当前所在页面Url', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'CurrentPage'
376 GO
377 
378 exec sp_addextendedproperty N'MS_Description', N'用户当前所在页面名称', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'CurrentPageTitle'
379 GO
380 
381 exec sp_addextendedproperty N'MS_Description', N'用户SessionId', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'SessionId'
382 GO
383 
384 exec sp_addextendedproperty N'MS_Description', N'客户端UA', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'UserAgent'
385 GO
386 
387 exec sp_addextendedproperty N'MS_Description', N'操作系统', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'OperatingSystem'
388 GO
389 
390 exec sp_addextendedproperty N'MS_Description', N'终端类型(0=非移动设备,1=移动设备)', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'TerminalType'
391 GO
392 
393 exec sp_addextendedproperty N'MS_Description', N'浏览器名称', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'BrowserName'
394 GO
395 
396 exec sp_addextendedproperty N'MS_Description', N'浏览器的版本', N'user', N'dbo', N'table', N'OnlineUsers', N'column', N'BrowserVersion'
397 GO
View Code

 

  7、添加后端相关表默认记录

 1 --添加部门记录
 2 INSERT INTO Branch (Code, Name, Notes, ParentId, Sort, Depth, Manager_Id, Manager_CName) 
 3     VALUES ('01', 'XX公司', '', 0, 1, 0, 1, 'admin')
 4 GO
 5 
 6 --添加职位记录
 7 INSERT INTO Position (Name, Branch_Id, Branch_Code, Branch_Name, PagePower, ControlPower, IsSetBranchPower, SetBranchCode, Manager_Id, Manager_CName) 
 8     VALUES ('软件开发工程师', 1, '01', 'XX公司', '', '', 1, '01', 1, 'admin')
 9 GO
10 
11 --添加管理员
12 INSERT INTO Manager (LoginName, LoginPass, LoginIp, LoginCount, Branch_Id, Branch_Code, Branch_Name, Position_Id, Position_Name, IsWork, IsEnable, CName, EName, Sex, Manager_Id, Manager_CName) 
13     VALUES ('admin', 'c3284d0f94606de1fd2af172aba15bf3', '127.0.0.1', 0, 1, '01', 'XX公司', '1', '软件开发工程师', 1, 1, 'admin', 'admin', '', 1, 'admin')
14 GO
View Code

 

  8、运行T4模板,为新加的表与修改的字段生成DAL层与BLL层代码

  

 

  9、登陆页cs文件代码(根据上面的流程图+代码中详细注释,大家应该很容易看明白)

  1 using System;
  2 using System.Collections;
  3 using System.Web;
  4 using System.Web.Caching;
  5 using DotNet.Utilities;
  6 using Solution.DataAccess.DataModel;
  7 using Solution.Logic.Managers;
  8 
  9 namespace Solution.Web.Managers.WebManage
 10 {
 11     public partial class Login : System.Web.UI.Page
 12     {
 13         protected void Page_Load(object sender, EventArgs e)
 14         {
 15             if (!IsPostBack)
 16             {
 17 
 18                 //进入登陆页面时判断是否是后台直接点击退出的,是的话加退出记录
 19                 LoginLogBll.GetInstence().UserExit();
 20 
 21                 #region 初始化用户Session变量
 22                 //在线用户生成的session标识
 23                 Session["UserHashKey"] = null;
 24                 //当前用户可访问的页面
 25                 Session["PagePower"] = null;
 26                 //当前用户页面中可使用的按钮控件
 27                 Session["ControlPower"] = null;
 28                 #endregion
 29             }
 30         }
 31 
 32         /// <summary>登录</summary>
 33         /// <param name="sender"></param>
 34         /// <param name="e"></param>
 35         protected void BtnLogin_Click(object sender, EventArgs e)
 36         {
 37             var ip = IpHelper.GetUserIp();
 38 
 39             #region 获取用户输入的参数,并进行数据初步处理
 40             //获取用户名,并进行危险字符过滤
 41             var username = StringHelper.Left(txtusername.Text, 50);
 42             //获取用户密码
 43             var userpass = txtpass.Text;
 44             //获取验证码
 45             var strCode = StringHelper.Left(txtcode.Text, 5);
 46             #endregion
 47 
 48             #region 初步验证
 49             //开发测试使用,不用每次都输入帐号与密码
 50             //username = "admin";
 51             //userpass = "admin";
 52             //strCode = "12345";
 53 
 54             //用户名验证
 55             if (string.IsNullOrEmpty(username.Trim()))
 56             {
 57                 txtusername.Focus();
 58                 JsHelper.Alert("用户名不能为空,请仔细检查您输入的用户名!");
 59                 return;
 60             }
 61             //密码验证
 62             if (string.IsNullOrEmpty(userpass.Trim()))
 63             {
 64                 txtpass.Focus();
 65                 JsHelper.Alert("密码不能为空,请仔细检查您输入的密码!");
 66                 return;
 67             }
 68 
 69             //验证码验证
 70             if (string.IsNullOrEmpty(strCode))
 71             {
 72                 txtcode.Focus();
 73                 JsHelper.Alert("验证码不能为空!");
 74                 return;
 75             }
 76             //判断验证码是否正确
 77             if (Session["vcode"] == null || !Session["vcode"].ToString().Equals(strCode, StringComparison.InvariantCultureIgnoreCase))
 78             {
 79                 SessionHelper.RemoveSession("vcode");
 80                 txtpass.Focus();
 81                 JsHelper.Alert("验证码错误!");
 82                 return;
 83             }
 84             else
 85             {
 86                 //验证码正确,删除验证码Session
 87                 SessionHelper.RemoveSession("vcode");
 88             }
 89             #endregion
 90 
 91             #region 数据库验证
 92 
 93             //通过用户给的用户名获取相关实体类
 94             var userinfo = Manager.SingleOrDefault(x => x.LoginName == username);
 95 
 96             //判断用户是否存在
 97             if (userinfo == null)
 98             {
 99                 LoginLogBll.GetInstence().Save(0, "账号【" + username + "】不存在,登录失败!");
100                 txtusername.Focus();
101                 JsHelper.Alert("用户名不存在,请仔细检查您输入的用户名!");
102                 return;
103             }
104 
105             //密码不匹配
106             if (!userinfo.LoginPass.Equals(Encrypt.Md5(Encrypt.Md5(userpass))))
107             {
108                 LoginLogBll.GetInstence().Save(userinfo.Id, "账号【" + userinfo.LoginName + "】的用户【" + userinfo.CName + "】登录失败!登录密码错误。");
109                 txtpass.Focus();
110                 JsHelper.Alert("您输入的用户密码错误!");
111                 return;
112             }
113 
114             if (userinfo.IsWork == 0)
115             {
116                 //添加用户登陆日志
117                 LoginLogBll.GetInstence().Save(userinfo.Id, "离职用户登录失败!用户【" + userinfo.CName + "】试图登录系统");
118                 JsHelper.Alert("您已经没有权限登录本系统!");
119                 return;
120             }
121 
122             //判断当前账号是否被启用
123             if (userinfo.IsEnable == 0)
124             {
125                 //添加登录日志记录
126                 LoginLogBll.GetInstence().Save(userinfo.Id, "账号【" + userinfo.LoginName + "】的用户【" + userinfo.CName + "】登录失败!用户账号被禁用。");
127 
128                 JsHelper.Alert("当前账号未被启用,请联系管理人员激活!");
129                 return;
130             }
131 
132             #endregion
133 
134             #region 存储在线用户资料
135 
136             #region 获取用户操作权限
137 
138             if (string.IsNullOrEmpty(userinfo.Position_Id))
139             {
140                 Session["PagePower"] = "";
141                 Session["ControlPower"] = "";
142 
143                 LoginLogBll.GetInstence().Save(0, "账号【" + username + "】未绑定职位,请管理员进行配置!");
144                 JsHelper.Alert("您的账号未绑定职位,请与管理员联系!");
145                 return;
146             }
147             else
148             {
149                 //获取用户权限并存储到用户Session里
150                 PositionBll.GetInstence().SetUserPower(userinfo.Position_Id);
151             }
152 
153             #endregion
154 
155             #region 当前用户在线信息
156             //当前时间
157             var localTime = DateTime.Now.ToLocalTime();
158             //创建客户端信息获取实体
159             var clientHelper = new ClientHelper(Request);
160 
161             //创建在线用户实体
162             var onlineUser = new Solution.DataAccess.Model.OnlineUsers();
163             //当前用户的Id编号
164             onlineUser.Manager_Id = userinfo.Id;
165             onlineUser.Manager_LoginName = userinfo.LoginName;
166             onlineUser.Manager_LoginPass = userinfo.LoginPass;
167             onlineUser.Manager_CName = userinfo.CName;
168             onlineUser.LoginTime = localTime;
169             onlineUser.LoginIp = ip;
170             //生成密钥
171             onlineUser.UserKey = RandomHelper.GetRndNum(32, true);
172             //Md5(密钥+登陆帐号+密码+IP+密钥.Substring(6,8))
173             onlineUser.Md5 =
174                 Encrypt.Md5(onlineUser.UserKey + onlineUser.Manager_LoginName + onlineUser.Manager_LoginPass +
175                             onlineUser.LoginIp + onlineUser.UserKey.Substring(6, 8));
176             onlineUser.UpdateTime = localTime;
177             onlineUser.Sex = userinfo.Sex;
178             onlineUser.Branch_Id = userinfo.Branch_Id;
179             onlineUser.Branch_Code = userinfo.Branch_Code;
180             onlineUser.Branch_Name = userinfo.Branch_Name;
181             onlineUser.Position_Id = userinfo.Position_Id;
182             onlineUser.Position_Name = userinfo.Position_Name;
183             onlineUser.CurrentPage = "";
184             onlineUser.CurrentPageTitle = "";
185             //SessionId
186             onlineUser.SessionId = Session.SessionID;
187             onlineUser.UserAgent = StringHelper.FilterSql(HttpContext.Current.Request.Headers["User-Agent"] + "");
188             onlineUser.OperatingSystem = clientHelper.GetSystem();
189             onlineUser.TerminalType = clientHelper.IsMobileDevice(onlineUser.UserAgent) ? 1 : 0;
190             onlineUser.BrowserName = clientHelper.GetBrowserName();
191             onlineUser.BrowserVersion = clientHelper.GetBrowserVersion();
192 
193             #endregion
194 
195             #region 记录当前用户UserId
196             //定义HashTable表里Key的名称UserId
197             string userHashKey = "";
198             //判断当前用户帐户是否支持同一帐号在不同地方登陆功能,取得用户在HashTable表里Key的名称
199             //不支持则
200             if (userinfo.IsMultiUser == 0)
201             {
202                 userHashKey = userinfo.Id + "";
203             }
204             //支持则
205             else
206             {
207                 userHashKey = userinfo.Id + "_" + onlineUser.SessionId;
208             }
209             //记录用户的HashTable Key
210             onlineUser.UserHashKey = userHashKey;
211             Session["UserHashKey"] = userHashKey;
212             #endregion
213 
214             #region 将在线用户信息存入全局变量中
215             //运行在线数据加载函数,如果缓存不存在,则尝试加载数据库中的在线表记录到缓存中
216             //——主要用于IIS缓存被应用程序池或其他原因回收后,对在线数据进行重新加载,而不会使所有用户都被迫退出系统
217             OnlineUsersBll.GetInstence().Load();
218 
219             //判断缓存中["OnlineUsers"]是否存在,不存在则直接将在线实体添加到缓存中
220             if (CacheHelper.GetCache("OnlineUsers") == null)
221             {
222                 //将当前用户信息添加到Hashtable中
223                 var hashtable = new Hashtable();
224                 hashtable.Add(userHashKey, onlineUser);
225                 //将在线列表(Hashtable)添中进系统缓存中
226                 CacheHelper.SetCache("OnlineUsers", hashtable);
227             }
228             //存在则将它取出HashTable并进行处理
229             else
230             {
231                 //直接从缓存中读取在线列表数据
232                 var hashtable = (Hashtable)CacheHelper.GetCache("OnlineUsers");
233 
234                 //判断当前用户是否存在在线表中,不存在则直接将当前用户的实体对象存储进HashTable
235                 if (hashtable[userHashKey] == null || hashtable.Count == 0)
236                 {
237                     hashtable.Add(userHashKey, onlineUser);
238                 }
239                 //存在则
240                 else
241                 {
242                     //添加用户下线记录
243                     LoginLogBll.GetInstence().Save(userHashKey, "用户【{0}】的账号已经在另一处登录,本次登陆下线!在线时间【{1}】");
244 
245                     //将HashTable里存储的前一登陆帐户移除
246                     OnlineUsersBll.GetInstence().Delete(this, x => x.UserHashKey == userHashKey);
247                     //移除缓存中的记录
248                     hashtable.Remove(userHashKey);
249 
250                     //将当前用户的实体对象存进在线缓存中
251                     hashtable.Add(userHashKey, onlineUser);
252                 }
253             }
254 
255             //将在线实体保存到数据库的在线表中
256             OnlineUsersBll.GetInstence().Save(this, OnlineUsersBll.GetInstence().Transform(onlineUser));
257 
258             //将用户信息表添加到缓存中,并且以150秒的轮询用户在线情况
259             //new PageBase().OnRemovedCallback为缓存回调函数,用于缓存失效、过期、删除或回收时,所触发的回调函数,执行相应操作
260             //缓存Key的前面加了"OnlineUsers_"标识,主要是用于清空缓存时区分用户缓存和其他系统缓存,不会将在线用户都清除下线
261             HttpRuntime.Cache.Insert("OnlineUsers_" + userHashKey, userHashKey, null, DateTime.MaxValue, TimeSpan.FromSeconds(150), CacheItemPriority.Default, new CacheItemRemovedCallback(OnRemovedCallback));
262 
263             //更新在线列表数据,将不在线人员删除
264             OnlineUsersBll.GetInstence().UpdateUserOnlineCount();
265 
266             #endregion
267 
268             #endregion
269 
270             #region 更新用户登陆信息
271 
272             userinfo.LoginIp = ip;
273             userinfo.LoginCount = userinfo.LoginCount++;
274             userinfo.LoginTime = localTime;
275 
276             ManagerBll.GetInstence().Save(this, userinfo, string.Format("用户【{0}】登陆成功,更新登陆信息", userinfo.CName));
277 
278             #endregion
279 
280             #region 添加用户登录成功日志
281             LoginLogBll.GetInstence().Save(userHashKey, string.Format("账号【{0}】的用户【{1}】登录成功", userinfo.LoginName, userinfo.CName));
282             #endregion
283 
284             #region 写Cookies
285             //写入用户的HashTable Key
286             CookieHelper.SetCookie(OnlineUsersTable.UserHashKey, userHashKey);
287             //写入加密值
288             CookieHelper.SetCookie(OnlineUsersTable.Md5, onlineUser.Md5);
289             #endregion
290 
291             //跳转进入主页面           
292             Response.Redirect("MainPage.aspx");
293         }
294 
295         #region 缓存回调函数
296         /// <summary>
297         /// 缓存回调函数,用于缓存失效、过期、删除或回收时,所触发的回调函数,执行相应操作
298         /// </summary>
299         /// <param name="key">缓存Key</param>
300         /// <param name="value">缓存值</param>
301         /// <param name="reason">触发的原因</param>
302         public void OnRemovedCallback(string key, object value, CacheItemRemovedReason reason)
303         {
304             if (key == null || value == null)
305                 return;
306 
307             //更新在线列表数据,将不在线人员删除
308             OnlineUsersBll.GetInstence().UpdateUserOnlineCount();
309 
310             //switch (reason)
311             //{
312             //    //相关联的缓存已经失效
313             //    case CacheItemRemovedReason.DependencyChanged:
314             //        break;
315 
316             //    //当前用户缓存已过期
317             //    case CacheItemRemovedReason.Expired:
318             //        //更新在线列表数据,将不在线人员删除
319             //        OnlineUsersBll.UpdateUserOnlineCount();
320 
321             //        break;
322 
323             //    //当前用户已被删除
324             //    case CacheItemRemovedReason.Removed:
325             //        break;
326 
327             //    //系统释放内存自动回收当前用户
328             //    case CacheItemRemovedReason.Underused:
329             //        //更新在线列表数据,将不在线人员删除
330             //        OnlineUsersBll.UpdateUserOnlineCount();
331 
332             //        break;
333 
334             //}
335         }
336         #endregion
337     }
338 }
View Code

 

  后端登陆页面界面随便在网上找了个改了一下,弄得很简陋,大家如果有好的UI可以发到我邮箱,我下次更新上去O(∩_∩)O

 

 

  点击下载:

由于框架不是非常成熟,很多朋友不是用来学习而是直接用到项目中,但不熟悉框架引起不少小问题,所以停止提供下载,有需要学习的可以到群共享里下,不便之处敬请谅解。

 

 

 版权声明:

  本文由AllEmpty原创并发布于博客园,欢迎转载,未经本人同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。如有问题,可以通过1654937@qq.com 联系我,非常感谢。

 

  发表本编内容,只要主为了和大家共同学习共同进步,有兴趣的朋友可以加加Q群:327360708 ,大家一起探讨。

 

  更多内容,敬请观注博客:http://www.cnblogs.com/EmptyFS/

 

 

 

 

posted @ 2014-06-05 12:41  AllEmpty  阅读(13907)  评论(24编辑  收藏  举报