读书人

第三周 -登进

发布时间: 2012-07-20 10:38:30 作者: rapoo

第三周 ---登入

实体

?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;
??????? }

?

读书人网 >其他相关

热点推荐