第三周 ---登入

实体

 public class UserModel
    {     
        public string UserName { set; get; }
        public string UserPassword { set; get; }
    }

DAL

public bool UserValidate(UserModel entity)
        {
            bool isPass = false;
            string sqlText = "select count(*) from ContactUser where UserName=@u and UserPassword=@p";

            int t = 0;
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand cmd = new SqlCommand(sqlText, conn);

                DaoHelper.AddParameter(cmd, "@u", entity.UserName);
                DaoHelper.AddParameter(cmd, "@p", entity.UserPassword);
                conn.Open();
                t = (int)cmd.ExecuteScalar();
            }
            if (t > 0)
            {
                isPass = true;
            }
            return isPass;
        }

页面

 

                var dal = new UserDAL();
                var model = new UserModel();   
                string u = txtUserName.Text;
         
                model.UserName = txtUserName.Text;
                model.UserPassword = txtUserPassword.Text;
                if (dal.UserValidate(model))
                {
                    FormsAuthentication.SetAuthCookie(u, false);
                    //Response.Write("认证通过");
                    Response.Redirect("/index.aspx");
                }
                else
                {
                    throw new Exception("用户名密码错误");
                }

 

继承控制访问

        需验证页继承此类

 

                在回发数据已加载到页服务器控件之后但在 OnLoad 事件之前,引发 PreLoad 事件。

          重载PreLoad

         protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
            LoginGuard();

        }

 

         private bool _CheckLogin = true;

        //是否验证默认为True

          public bool CheckLogin
        {
            get { return _CheckLogin; }
            set { _CheckLogin = value; }

 

 

       //已经验证通过或不必要验证则pass

                protected virtual void LoginGuard()
        {
            if (!User.Identity.IsAuthenticated && CheckLogin)
                Response.Redirect("/Login.aspx");
        }

      登入页不必验证则

      public Login()
        {
            CheckLogin = false;
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值