public ActionResult Login(AccountViewModel model) { SecurityHandler securityHandler = new SecurityHandler(); UserAccount account = securityHandler.Login(model.UserName, model.Password, true); if (account != null) { FormsAuthentication.SetAuthCookie(account.UserName, model.IsRemember); FormsAuthentication.SetAuthCookie(Convert.ToString(account.UserID), model.IsRemember); var authTicket = new FormsAuthenticationTicket(1, account.UserName, DateTime.Now, DateTime.Now.AddMinutes(20), false, account.RoleList); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); HttpContext.Response.Cookies.Add(authCookie); //Based on the Role we can transfer the user to different page //return RedirectToAction("Index", "Home"); return(RedirectToAction("Index", "Home")); } return(View("Index")); }
protected void btLogin_Click(object sender, EventArgs e) { try { if (Session["check"] == null) { this.tbCaptcha.Text = string.Empty; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script language=\"javascript\">alert(\"验证码失效!\")</script>"); return; } if (tbCaptcha.Text != Session["check"].ToString()) { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script language=\"javascript\">alert(\"验证码错误!\")</script>"); this.tbCaptcha.Text = string.Empty; return; } string usercode = Server.HtmlEncode(tbUserName.Text); string passWord = tbPassWord.Text; //进行用户登录,security.LoginResult为null或者security.LoginResult.IsPassed && security.LoginResult.AuthorizationCode != "" //都是登录失败 SecurityHandler security = SecurityHandler.Login(usercode, passWord); if (security.LoginResult.SystemCode == null) //判断存不存在 { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script language=\"javascript\">alert(\"用户名或密码错误!\")</script>"); this.tbUserName.Text = string.Empty; this.tbPassWord.Text = string.Empty; this.tbCaptcha.Text = string.Empty; return; } var aa = SecurityHandler.LoginOn(security.LoginResult.AuthorizationCode).GetCurrentUserInfo(); Dictuser user = new Dictuser(); user.Usercode = aa.USERNAME; user = new DictuserService().GetDictuserInfoByUserCode(user); if (user != null) { UserInfo userInfo = new UserInfo(); userInfo.AuthorizationCode = security.LoginResult.AuthorizationCode; userInfo.userCode = user.Usercode; userInfo.userName = user.Username; userInfo.userId = Convert.ToInt32(user.Dictuserid); userInfo.loginTime = DateTime.Now; userInfo.joinLabidstr = user.Joinlabid; userInfo.dictlabid = user.Dictlabid; userInfo.joinDeptstr = user.Joindeptid; userInfo.dictlabdeptid = user.Dictlabdeptid; userInfo.sysSetting = GetSysSetting(); Session["UserInfo"] = userInfo; } if (security.LoginResult.IsPassed && security.LoginResult.AuthorizationCode != "") { //这里的Cookie名字不能更改 HttpCookie cookie = new HttpCookie("authorizationcode"); cookie.Value = security.LoginResult.AuthorizationCode; TimeSpan ts = new TimeSpan(1, 0, 0, 0); cookie.Expires = DateTime.Now.Add(ts);//添加作用时间 Response.AppendCookie(cookie); if (!RegexPassWordSecurity(passWord)) { ClientScript.RegisterStartupScript(this.GetType(), "redirectToChangePassword", "<script> alert('您的密码安全性较弱,请重新修改密码'); window.location.href='EditPassword.aspx';</script>"); return; } Response.Redirect("Main.aspx", false); //////PageContext.RegisterStartupScript("top.location.href = 'Main.aspx';"); //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script language=\"javascript\">top.location.href = 'Main.aspx';</script>"); } else { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "message", "<script language=\"javascript\">alert(\"用户名或密码错误!\")</script>"); this.tbUserName.Text = string.Empty; this.tbPassWord.Text = string.Empty; this.tbCaptcha.Text = string.Empty; return; } } catch (Exception ex) { Alert.ShowInTop(ex.Message, "体检系统"); } }