using MESClassLibrary.BLL.Log; using MESClassLibrary.BLL.User; using MESClassLibrary.Model; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace MESWebSite { public partial class Login : System.Web.UI.Page { UserBLL userclass = new UserBLL(); UserModel md = new UserModel(); private static readonly string passwordPattern = @"^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!@#$%^&*()_+/,.?<>:;'\{}=])[a-zA-Z0-9!@#$%^&*()_+/,.?<>:;'\{}=]{8,12}$"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } /// /// 登录按钮 /// /// /// protected void btnLogin_Click(object sender, EventArgs e) { try { string _loginName = this.LoginName.Text.Trim(); string _loginpassword = this.LoginPassword.Text.Trim(); //if (!IsValid(_loginpassword)) //{ // ClientScript.RegisterStartupScript(ClientScript.GetType(), "错误", ""); //} //else //{ // UserNameLogin(_loginName, _loginpassword); //} UserNameLogin(_loginName, _loginpassword); } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); } } public static bool IsValid(string password) { //使用正则表达式判断密码是否符合要求 return Regex.IsMatch(password, passwordPattern); } /// /// 登录方法 /// /// /// public void UserNameLogin(string _loginName, string _loginpassword) { try { int errTimes = 0; md.Username = _loginName; md.Password = _loginpassword; DateTime lastLoginTime; if (userclass.Login(md)) { DataTable dt_user = new DataTable(); dt_user = userclass.SearchUserInfoByUserName(md.Username); md.ErrTimes = 0; userclass.UpdateUserInfo(md); HttpCookie cookie = new HttpCookie("LoginUserInfo"); cookie.HttpOnly = true; cookie["UserID"] = dt_user.Rows[0]["UserID"].ToString().Trim(); cookie["UserName"] = _loginName; cookie["PassWord"] = _loginpassword; cookie["RealName"] = HttpUtility.UrlEncode(dt_user.Rows[0]["RealName"].ToString().Trim(), System.Text.Encoding.UTF8); //cookie.Expires = DateTime.Now.AddDays(14); Response.Cookies.Add(cookie); Response.Redirect("Manage/index.aspx", false); } else { DataTable dt_user = new DataTable(); dt_user = userclass.SearchUserInfoByUserName(md.Username); if (dt_user != null && dt_user.Rows.Count > 0) { errTimes = Convert.ToInt32(dt_user.Rows[0]["ErrTimes"].ToString()); lastLoginTime = Convert.ToDateTime(dt_user.Rows[0]["LastLoginTime"].ToString()); if (errTimes >= 3 && (DateTime.Now - lastLoginTime).TotalMinutes < 15) { md.ErrTimes = errTimes + 1; userclass.UpdateUserInfo(md); ClientScript.RegisterStartupScript(ClientScript.GetType(), "错误", ""); } else { md.ErrTimes = errTimes + 1; userclass.UpdateUserInfo(md); ClientScript.RegisterStartupScript(ClientScript.GetType(), "错误", ""); } } } #region 注销 //if (userclass.Login(md)) //{ // #region 下次自动登录-暂时注销 // //if (AutoLogin.Checked) // //{ // // HttpCookie cookie = new HttpCookie("LoginUserInfo"); // // cookie["UserName"] = _loginName; // // cookie["PassWord"] = _loginpassword; // // cookie.Expires = DateTime.Now.AddDays(14); // // Response.Cookies.Add(cookie); // //} // //Session.Add("LoginUserInfo", md); // #endregion // Response.Redirect("Manage/index.aspx", false); //} //else //{ // ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", ""); //} #endregion } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); } } } }