示例#1
0
        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            string userName = Request.Form["txtUserName"];
            string password = Request.Form["txtPassword"];

            if (string.IsNullOrEmpty(userName))
            {
                Response.Write("<script>alert('必须输入用户名!');</script>");
                return;
            }
            if (string.IsNullOrEmpty(password))
            {
                Response.Write("<script>alert('必须输入密码!');</script>");
                return;
            }

            // 密码MD5加密
            password = FrmUtil.CalculateMD5Hash(password);

            using (var context = ContextFactory.CreateContext <User>())
            {
                var user = context.Set <User>()
                           .FirstOrDefault(u => u.UserName == userName && u.Password == password);
                if (user != null)
                {
                    Session["MyUserName"] = userName;
                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    Response.Write("<script>alert('用户名或密码错误!');</script>");
                }
            }
        }
示例#2
0
        /// <summary>
        /// 保存用户信息
        /// </summary>
        private string saveUserInfo()
        {
            string userName = hidID.Contains("UserName") ? hidID["UserName"].ToString() : "";

            using (var context = ContextFactory.CreateContext <User>())
            {
                User user;
                if (hidID.Contains("UserName"))
                {
                    user = context.Set <User>().FirstOrDefault(u => u.UserName == userName);
                }
                else
                {
                    user = new User();
                }

                user.UserName = txtUserName.Text;
                user.Name     = txtName.Text;
                if (!txtPassword.Text.Equals(user.Password))
                {
                    user.Password = FrmUtil.CalculateMD5Hash(txtPassword.Text); // MD5加密
                }
                user.Password = txtPassword.Text;
                user.Memo     = txtMemo.Text;

                if (!hidID.Contains("UserName"))
                {
                    context.Set <User>().Add(user);
                }

                int rtn = context.SaveChanges();
                if (rtn != 1)
                {
                    return("保存失败!");
                }
                else
                {
                    return(null);
                }
            }
        }