学习PetShop3.0(1)用户登陆(SignIn.aspx)

.net已经到了2005,微软的PetShop3.0也已经到了4.0,现在还学习PetShop3.0,纯属弥补以前学习.net漏掉的步骤。毕竟有些东西,不看的话,还是不知道的。网上关于PetShop的文章多是从架构或者语言特性方面来说的,所以决定先从最基本的页面分析开。

登陆页面SignIn.aspx。一个img用来注册,转到新的注册页面。有一个imagebutton,用来提交页面。两个img,都设置了alertText似乎是个好的习惯。提交按钮调用后台SubmitClicked方法。

1)   (signIn.aspx.csPetShop3.0是业务实体和业务逻辑分开的,并且在表示层上也有逻辑处理。例如WebComponents.CleanString就是用来进行输入的合法性检验。

string userId = WebComponents.CleanString.InputText(txtUserId.Text, 50);

string password = WebComponents.CleanString.InputText(txtPassword.Text, 50);

     首先,判断输入字符串长度,超过最长长度则截取。然后替换掉一些危险的字符’ ” , <, >,’ ’

     for (int i = 0; i < inputString.Length; i++) {

                       switch (inputString[i]) {

                            case '"':

                                 retVal.Append("&quot;");

                                 break;

                            case '<':

                                 retVal.Append("&lt;");

                                 break;

                            case '>':

                                 retVal.Append("&gt;");

                                 break;

                            default:

                                 retVal.Append(inputString[i]);

                                 break;

                       }

                   }

 

                   // Replace single quotes with white space

                   retVal.Replace("'", " ");

2)   (PetShop.Web.ProcessFolw.AccountController.cs)接下来进行正式的帐户登陆。accountController.ProcessLogin(userId, password),判断返回值来表示是否登陆成功。新建业务层的account,调用account.SignIn(userId, password)来登陆帐户。

通过判断myAccountInfo来判断是否成功登陆。

Account account = new Account();

AccountInfo myAccountInfo = account.SignIn(userId, password);

3)   似乎是PetShop3.0 的亮点之一,充分使用了语言本身的特性,动态创建Dao层,避免了原来那些创建模式的弊端。(PetShop.BLL.Account.cs)account.SignIn的方法如下。

public AccountInfo SignIn(string userId, string password) {

              if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))

                   return null;

              IAccount dal = PetShop.DALFactory.Account.Create();

              AccountInfo account = dal.SignIn(userId, password);

              return account;

         }

 这里PetShop.DALFactory.Account.Create()利用了依赖注入。利用WebConfig读去要新建的类的名字。<add key="WebDAL" value="PetShop.SQLServerDAL" />表示该类支持SQL.新建一个DAL层的Account的实例。

public static PetShop.IDAL.IAccount Create()

         {            

              string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];

              string className = path + ".Account";

              return (PetShop.IDAL.IAccount) Assembly.Load(path).CreateInstance(className);

         }

4)   (PetShop.DALFactory.Account.cs) AccountInfo account = dal.SignIn(userId, password)开始数据层的访问。传入用户名和密码。如果有返回值说明用户存在。返回一个AccountInfo实体类。

5)   (回到PetShop.Web.ProcessFolw.AccountController.cs)成功登陆开始页面导向。返回到原来登陆的页面。并把帐户信息存入session

     if (myAccountInfo != null) {

                   HttpContext.Current.Session[ACCOUNT_KEY] = myAccountInfo;

                   if (FormsAuthentication.GetRedirectUrl(userId, false).EndsWith(URL_DEFAULT)) {

                       FormsAuthentication.SetAuthCookie(userId, false);

                       HttpContext.Current.Response.Redirect(URL_ACCOUNTSIGNIN, true);

                   }else{

FormsAuthentication.SetAuthCookie(userId, false);

         HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userId, false), true);

                   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值