在petshop3.0中有一个MyAccount.aspx页面,此页面对用户不同的操作做出不同的提示,比如新创建用户,用户更新,用户登录.其中有三个参数
private
const
string
ACTION_CREATE
=
"
create
"
;
private
const
string
ACTION_UPDATE
=
"
update
"
;
private
const
string
ACTION_SIGN_IN
=
"
signIn
"
;
private
const
string
TITLE_CREATE
=
"
Create Account
"
;
private
const
string
TITLE_UPDATE
=
"
Edit Account
"
;
private
const
string
TITLE_SIGN_IN
=
"
Sign In
"
;
private
const
string
MSG_CREATE
=
"
Your account was successfully created.
"
;
private
const
string
MSG_UPDATE
=
"
Your account was successfully updated.
"
;
private
const
string
MSG_SIGN_IN
=
"
Welcome to the .NET Pet Shop Demo.
"
;
通过检测Request["action"]来传递,然后做出判断,显示不同提示信息
override
protected
void
OnLoad(EventArgs e)
{
string pageAction = WebComponents.CleanString.InputText(Request["action"], 20);
switch(pageAction)
{
case ACTION_CREATE:
lblMessage.Text = MSG_CREATE;
break;
case ACTION_UPDATE:
lblMessage.Text = MSG_UPDATE;
break;
case ACTION_SIGN_IN:
lblMessage.Text = MSG_SIGN_IN;
break;
}
}
在 WEB项目中的ProcessFlow文件夹中有个AccountController.cs帐户管理类.它引用了已经封装好了的Account类中的方法.然后
这个类根据用户不同的操作进行导向,并赋予权限
1/**//// <summary>
2 /// 控制帐户相关操作的流程
3 /// </summary>
4 public class AccountController {
5 // 导航内容
6
7 private const string ACCOUNT_KEY = "ACCOUNT_KEY";
8 private const string URL_DEFAULT = "default.aspx";
9 private const string URL_SIGNIN = "SignIn.aspx";
10 private const string URL_ACCOUNTCREATE = "MyAccount.aspx?action=create";
11 private const string URL_ACCOUNTSIGNIN = "MyAccount.aspx?action=signIn";
12 private const string URL_ACCOUNTUPDATE = "MyAccount.aspx?action=update";
13
14 /**//// <summary>
15 /// 构造函数
16 /// </summary>
17 public AccountController(){
18 }
19
20 /**//// <summary>
21 /// 验证登陆系统
22 /// 判断是否登录成功
23 /// </summary>
24 /// <param name="userId">User name the customer is authenticating with</param>
25 /// <param name="password">Password the customer is using</param>
26 /// <returns>true if the login is successful</returns>
27 public bool ProcessLogin(string userId, string password){
28
29 // 使用帐户业务逻辑层来登录
30 Account account = new Account();
31 AccountInfo myAccountInfo = account.SignIn(userId, password);
32
33 //登录成功,在Session中存储状态并且跳转
34 if (myAccountInfo != null) {
35 HttpContext.Current.Session[ACCOUNT_KEY] = myAccountInfo;
36
37 // 决定用户跳转到哪儿
38 // 返回到登录成功的提示页面
39 if (FormsAuthentication.GetRedirectUrl(userId, false).EndsWith(URL_DEFAULT)) {
40
41 FormsAuthentication.SetAuthCookie(userId, false);
42 HttpContext.Current.Response.Redirect(URL_ACCOUNTSIGNIN, true);
43
44 }else{
45 // 返回上一个页面
46 FormsAuthentication.SetAuthCookie(userId, false);
47
48 HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userId, false), true);
49 }
50
51 return true;
52
53 }else {
54 // 登录失败
55 return false;
56 }
57 }
58
59 public bool CreateAccount(AccountInfo newAccountInfo){
60
61 try {
62 // 创建一个新帐户逻辑对象
63 Account account = new Account();
64
65 // 调用Insert方法
66 account.Insert(newAccountInfo);
67
68 // 在Session中存储帐户信息,在cookie中存储验证信息
69 HttpContext.Current.Session[ACCOUNT_KEY] = newAccountInfo;
70 FormsAuthentication.SetAuthCookie(newAccountInfo.UserId, false);
71
72 //跳转到注册成功页面
73 HttpContext.Current.Response.Redirect(URL_ACCOUNTCREATE, true);
74
75
76 }catch {
77 return false;
78 }
79
80 return true;
81 }
82
83 /**//// <summary>
84 /// 更新帐户信息方法
85 /// </summary>
86 /// <param name="updatedAccountInfo">Updated account information</param>
87 public void UpdateAccount(AccountInfo updatedAccountInfo){
88
89 // 创建帐户逻辑类
90 Account account = new Account();
91
92 // 调用更新方法
93 account.Update(updatedAccountInfo);
94
95 //将更改后的信息保存在session中
96 HttpContext.Current.Session[ACCOUNT_KEY] = updatedAccountInfo;
97
98 //返回更新成功提示
99 HttpContext.Current.Response.Redirect(URL_ACCOUNTUPDATE, true);
100
101 }
102
103 /**//// <summary>
104 /// 得到一个登录后的顾客的帐户信息
105 /// 假设信息存储在session里
106 /// 如果没有找到要用的信息,则重新登录
107 /// </summary>
108 /// <returns>The account info for the currently logged in user</returns>
109 public AccountInfo GetAccountInfo(bool required){
110 AccountInfo myAccount = (AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY];
111
112 if (myAccount == null){
113 if(required){
114 HttpContext.Current.Response.Redirect(URL_SIGNIN, true);
115
116 }
117 return null;
118 }else{
119 return myAccount;
120 }
121 }
122
123 /**//// <summary>
124 /// 如果我们知道的话就找到用户的关注类型
125 /// 假设信息存储在session里
126 /// </summary>
127 /// <returns>The customers favourite category</returns>
128 public string GetFavouriteCategory(){
129
130 AccountInfo myAccount = (AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY];
131
132 if (myAccount != null && myAccount.IsShowFavorites) {
133 return myAccount.Category;
134 }else{
135 return null;
136 }
137 }
138
139 /**//// <summary>
140 /// 退出系统方法
141 /// 用户退出系统则清空seesion,以及他们的验证将被重置
142 /// </summary>
143 public void LogOut(){
144
145 // 清除验证信息
146 FormsAuthentication.SignOut();
147 // 清除session内容
148 HttpContext.Current.Session.Clear();
149 // 取消当前会话
150 HttpContext.Current.Session.Abandon();
151 }
152 }
接着看另外三个页面,登录页面,创建帐户页面,修改帐户信息页面
登录页面很简单,引用了刚才的帐户管理类
ProcessFlow.AccountController accountController
=
new
ProcessFlow.AccountController();
if
(
!
accountController.ProcessLogin(userId, password))
{
// If we fail to login let the user know
valUserId.ErrorMessage = MSG_FAILURE;
valUserId.IsValid = false;
}
创建帐户页面和修改帐户信息页面比较相似,修改帐户信息页面无法修改密码.
当创建帐户页面时需要同时向三张表插入数据,分别是用户基本信息表,用户登录表,用户配置表
修改信息页面只需要修改两张表, 用户基本信息表和用户配置表,因为用户无法更改用户名和密码,所以用户登录表无须改变.
其中注册页面的地址栏和信息配置选项是用用户控件来完成的
当然最后还有注销页面
override
protected
void
OnLoad(EventArgs e)
{
// Create an instance of the account controller
ProcessFlow.AccountController accountController = new ProcessFlow.AccountController();
// Tell the controller that the user is logging out
accountController.LogOut();
}