加入了设计模式的概念。
之前的设计没有考虑类的扩展性,如果用户的属性一改变,那得重新修改类,所以这次改进了一下。
1using System;
2using System.Web;
3using System.Web.UI;
4using System.Web.Security;
5
6namespace FbSoft
7{
8 用户抽象类#region 用户抽象类
9 internal abstract class AbstractUser
10 {
11 private string m_UserId;
12 private int m_Id;
13 private string m_Name;
14
15 protected string UserData;
16
17 internal AbstractUser()
18 {
19 }
20
21 /**//// <summary>
22 /// 帐号
23 /// </summary>
24 internal string UserId
25 {
26 get{return m_UserId;}
27 set{m_UserId = value;}
28 }
29 /**//// <summary>
30 /// 顺序号
31 /// </summary>
32 internal int Id
33 {
34 get{return m_Id;}
35 set{m_Id = value;}
36 }
37 /**//// <summary>
38 /// 中文姓名
39 /// </summary>
40 internal string Name
41 {
42 get{return m_Name;}
43 set{m_Name = value;}
44 }
45
46 /**//// <summary>
47 /// 获得当前登录者的信息
48 /// </summary>
49 internal virtual void GetCurrent()
50 {
51 HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
52 if(authCookie == null)
53 {
54 UserData = null;
55 }
56 FormsAuthenticationTicket authTicket = null;
57 try
58 {
59 authTicket = FormsAuthentication.Decrypt(authCookie.Value);
60 }
61 catch
62 {
63 throw;
64 }
65 if (authTicket == null)
66 {
67 UserData = null;
68 }
69 UserData = authTicket.UserData;
70 }
71
72 /**//// <summary>
73 /// 保存登录用户的信息
74 /// </summary>
75 internal virtual void Logon()
76 {
77 FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, UserId, DateTime.Now, DateTime.Now.AddMinutes(30), false, ToString());
78 string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
79 HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
80 HttpContext.Current.Response.Cookies.Add(authCookie);
81 }
82
83 /**//// <summary>
84 /// 移除用户信息
85 /// </summary>
86 internal virtual void SignOut()
87 {
88 FormsAuthentication.SignOut();
89 }
90 }
91 #endregion
92
93 用户具体类#region 用户具体类
94 internal class User : AbstractUser
95 {
96 private string m_Purview;
97
98 internal User() : base()
99 {
100 }
101 /**//// <summary>
102 /// 权限
103 /// </summary>
104 internal string Purview
105 {
106 get{return m_Purview;}
107 set{m_Purview = value;}
108 }
109
110 /**//// <summary>
111 /// 得到用户数据
112 /// </summary>
113 /// <returns></returns>
114 public override string ToString()
115 {
116 string[] strData = new string[]{this.Id.ToString(), this.UserId, this.Name, this.Purview};
117 return string.Join("\t", strData);
118 }
119
120 /**//// <summary>
121 /// 获取当前用户
122 /// </summary>
123 /// <returns></returns>
124 internal override void GetCurrent()
125 {
126 base.GetCurrent();
127 if(base.UserData == null)return;
128 string[] strData = base.UserData.Split('\t');
129 Id = int.Parse(strData[0]);
130 UserId = strData[1];
131 Name = strData[2];
132 Purview = strData[3];
133 }
134 }
135 #endregion
136}
137
2using System.Web;
3using System.Web.UI;
4using System.Web.Security;
5
6namespace FbSoft
7{
8 用户抽象类#region 用户抽象类
9 internal abstract class AbstractUser
10 {
11 private string m_UserId;
12 private int m_Id;
13 private string m_Name;
14
15 protected string UserData;
16
17 internal AbstractUser()
18 {
19 }
20
21 /**//// <summary>
22 /// 帐号
23 /// </summary>
24 internal string UserId
25 {
26 get{return m_UserId;}
27 set{m_UserId = value;}
28 }
29 /**//// <summary>
30 /// 顺序号
31 /// </summary>
32 internal int Id
33 {
34 get{return m_Id;}
35 set{m_Id = value;}
36 }
37 /**//// <summary>
38 /// 中文姓名
39 /// </summary>
40 internal string Name
41 {
42 get{return m_Name;}
43 set{m_Name = value;}
44 }
45
46 /**//// <summary>
47 /// 获得当前登录者的信息
48 /// </summary>
49 internal virtual void GetCurrent()
50 {
51 HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
52 if(authCookie == null)
53 {
54 UserData = null;
55 }
56 FormsAuthenticationTicket authTicket = null;
57 try
58 {
59 authTicket = FormsAuthentication.Decrypt(authCookie.Value);
60 }
61 catch
62 {
63 throw;
64 }
65 if (authTicket == null)
66 {
67 UserData = null;
68 }
69 UserData = authTicket.UserData;
70 }
71
72 /**//// <summary>
73 /// 保存登录用户的信息
74 /// </summary>
75 internal virtual void Logon()
76 {
77 FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, UserId, DateTime.Now, DateTime.Now.AddMinutes(30), false, ToString());
78 string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
79 HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
80 HttpContext.Current.Response.Cookies.Add(authCookie);
81 }
82
83 /**//// <summary>
84 /// 移除用户信息
85 /// </summary>
86 internal virtual void SignOut()
87 {
88 FormsAuthentication.SignOut();
89 }
90 }
91 #endregion
92
93 用户具体类#region 用户具体类
94 internal class User : AbstractUser
95 {
96 private string m_Purview;
97
98 internal User() : base()
99 {
100 }
101 /**//// <summary>
102 /// 权限
103 /// </summary>
104 internal string Purview
105 {
106 get{return m_Purview;}
107 set{m_Purview = value;}
108 }
109
110 /**//// <summary>
111 /// 得到用户数据
112 /// </summary>
113 /// <returns></returns>
114 public override string ToString()
115 {
116 string[] strData = new string[]{this.Id.ToString(), this.UserId, this.Name, this.Purview};
117 return string.Join("\t", strData);
118 }
119
120 /**//// <summary>
121 /// 获取当前用户
122 /// </summary>
123 /// <returns></returns>
124 internal override void GetCurrent()
125 {
126 base.GetCurrent();
127 if(base.UserData == null)return;
128 string[] strData = base.UserData.Split('\t');
129 Id = int.Parse(strData[0]);
130 UserId = strData[1];
131 Name = strData[2];
132 Purview = strData[3];
133 }
134 }
135 #endregion
136}
137