您的位置:首页 > 其它

电子商务之数据存储流程(五)

2009-11-23 08:53 197 查看
  首先我想还是给大家过一遍数据存储的流程吧。选个最简单的例子,用户登录,将用户从数据库中取出来。

首先需要在SQL数据库中编写存储过程,选存储过程+传递参数用SqlParameter是因为,除非是ADO.NET有漏洞,那么就绝对不会发生SQL注入。据我所知SQL注入发生在借用用户输入拼接生成SQL语句的地方。其次执行数据操作的性能也提高了。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Shop.Common;
using Shop.BusinessLogic;

public partial class Login : BasePage
{
protected void Page_Load( object sender , EventArgs e )
{
textUsername.Focus();
}

protected void commandLogin_Click( object sender , EventArgs e )
{
if ( IsValid )
{
EndUserEntity enduser = new EndUserEntity();
ProcessEndUserLogin processlogin = new ProcessEndUserLogin();

enduser.UserContactInformation.Email = textUsername.Text;
enduser.Password = textPassword.Text;
processlogin.EndUser = enduser;

try
{
processlogin.Invoke();
}
catch
{
Response.Redirect( "ErrorPage.aspx" );
}

if ( processlogin.IsAuthenticated )
{
Response.Cookies["Authenticated"].Value = "True";

base.CurrentEndUser = processlogin.EndUser;

if ( Request.Cookies["ReturnURL"] != null )
{
Response.Redirect( Request.Cookies["ReturnURL"].Value );
}
else
{
Response.Redirect("Account/CustomerOrders.aspx");
}
}
else
{
labelMessage.Text = "Invalid login!";
}
}
}
}

这里我想要说明一点就是try...catch的用法,用得非常好,如果 processlogin.Invoke();就是我们刚才写的那么多代码里发生的错误或异常在这里就可以轻松捕抓到并向用户提示页面出错,我个人以前在这方面会忽略,所以认为这里很经典
,注意到这里用户的密码没有加密,我们也可自己调用System.Web.Security.FormsAuthencation.HashPasswordForStoringInConfigFile()进行加密。下面的一些代码会涉及到很多内容我会在下面的内容和大家一起分析(注意到页面继承的BasePage类,而不是我们经常看到System.Web.UI.Page类)另外要强调的是,要注意引用这些程序集,例如我们在Shop.DataAccess添加引用项目Shop.Common类库,Shop.BusinessLogic添加引用Shop.DataAccess类库以及Shop.Common类库等等相信大家这个都会吧 -_-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: