MVC设计模式在asp.net用户登陆中设计和运用

MVC设计模式使得业务逻辑和界面完全分离,使一个系统的业务流程和架构更清晰,使系统具有高重用性,可适用性和低耦合性.因此,
系统的可维护性和健壮性更强,便于根据实际需要来改变数据层和业务规则.下面结合具体实际使用MVC模式设计用户登陆验证的模块.
用户登陆验证的原理就不再阐述.
这里我采用SQL SERVER2000做数据库,建立如下数据表:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[userinfo]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
drop table [dbo].[userinfo]
GO
CREATE TABLE [dbo].[userinfo] (
[username] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[password] [char] (30) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
建立如下存储过程:
/*
存储过程checklogin
创建者:阿力
创建日期:2009年3月10日
作用:对用户输入的用户名和密码进行判断
参数说明:
@username  用户名
@password  用户密码
@IsValid 输出参数
*/
CREATE PROCEDURE checklogin
(
@username nvarchar(20),
@assword nvarchar(15) ,
@IsValid Int  output)
AS
if (select count(*) from userinfo where username=#username and  password=#password)=1
  begin
    select @IsValid = 1
  end
else
    select @IsValid = 0
return
GO
备注:将上面#换成@
将上面SQL语句在SQL Server2000里的查询分析器里执行
建立用户模型层:
using System;
using System.Collections.Generic;
using System.Text;
//用户登陆信息模型层,相当于Struts中MODEL
//2009年3月10日
namespace Model
{
   public class LoginBean
    {
        public LoginBean()
         {}
        #region Model
        private string _username;//用户名
        private string _password;//密码
        public string username
        {
            set
            {  
                _username= value;
            }
            get
            {
                return _username;  
            }
        }
        public string password
        {
            set
            {
                _password = value;
            }
            get
            {
                return _password;
            }
        }
        #endregion Model
}
}

建立用户登陆验证的数据访问层:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Model;
//用户信息数据访问层,相当于Struts中的DAO
//2009年3月10日
namespace DAL
{
    public class UserInfoDao
    {  
        public bool CheckUser(string name, string pwd) //返回布尔类型
        {
            bool authenticated = false;
            //从文件Web.config中读取连接字符串
            string ST_sqldb = ConfigurationSettings.AppSettings["ConnectionString"];
            //创建Command对象
            SqlCommand ST_mycommand = new SqlCommand();
            //连接数据库
            ST_mycommand.Connection = new SqlConnection(ST_sqldb);
            try
            {
                ST_mycommand.Connection.Open();
                //调用存储过程checklogin检验帐户的有效性
                ST_mycommand.CommandText = "checklogin";
                ST_mycommand.CommandType = CommandType.StoredProcedure;
                SqlParameter Name = new SqlParameter("@username", SqlDbType.NVarChar, 20);
                Name.Value = name.Trim();
                ST_mycommand.Parameters.Add(Name);
                SqlParameter Password = new SqlParameter("@password", SqlDbType.NVarChar, 15);
                Password.Value = pwd.Trim();
                ST_mycommand.Parameters.Add(Password);
                SqlParameter IsValid = new SqlParameter("@IsValid", SqlDbType.Int);
                IsValid.Direction = ParameterDirection.Output;
                ST_mycommand.Parameters.Add(IsValid);
                ST_mycommand.ExecuteNonQuery();
                if (((int)IsValid.Value) == 1)
                {
                    //帐户有效
                    authenticated = true;
                }
            }
            catch (Exception exc)
            {
                throw (exc);
            }
            finally
            {
                ST_mycommand.Connection.Close();
            }
            //返回布尔值
            return authenticated;
        }
    }
}

建立用户业务逻辑层:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Model;
using DAL;
//用户基本信息控制层,相当于Struts中的Controller
//2009年3月10日
namespace BLL
{
    public class UserInfoBLL
    {
        UserInfoDao dao = new UserInfoDao();
        //验证用户登陆,布尔型
        public bool CheckUserInfo(string name,string pwd)
        {
            return dao.CheckUser(name,pwd);
        }
    }
}

建立页面,在cs文件中调用以上函数:
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 BLL;
using Model;
public partial class bm_login : System.Web.UI.Page
{
    UserInfoBLL bll = new UserInfoBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public void WriteSession(string userID)
    {
       Session["Login"] = userID;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = Encode(Request.Form["name"]);
        string pwd = Encode(Request.Form["pwd"]);
        if (bll.CheckUserInfo(name, pwd) == true)
        {
            WriteSession(name);//写入session
            Response.Redirect("wybm.aspx");
        }
        else
        {
            Response.Write("<script>alert('用户名和密码错误')</script>");
        }
    }
    public static string Encode(string str)
    {
        str = str.Replace("&", "&amp;");
        str = str.Replace("'", "''");
        str = str.Replace("/"", "&quot;");
        str = str.Replace(" ", " ");
        str = str.Replace("<", "&lt;");
        str = str.Replace(">", "&gt;");
        str = str.Replace("/n", "<br>");
        return str;
    }
}
好了,程序到此全部写完,该代码在WINXP+IIS5.1+VS2005测试下通过,程序结构良好,分层清晰,健壮性较强,便于维护和管理.
其实,我们还可以进一步分层,增加2层:抽象数据访问工厂(DALFactory)和数据访问接口(IDAL),这方面我会在以后的代码作品中绍.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值