예제 #1
0
        public ActionResult CheckLogin(string username, string password, string code)
        {
            string ss = Md5.GetMD5(code.ToLower());

            try
            {
                if (SessionHelper.GetSession(SessionKey.session_verifycode.ToString()) == string.Empty || Md5.GetMD5(code.ToLower()) != SessionHelper.GetSession(SessionKey.session_verifycode.ToString()))
                {
                    string dds = SessionHelper.GetSession(SessionKey.session_verifycode.ToString());
                    string dd  = Session[SessionKey.session_verifycode.ToString()].ToString();
                    throw new Exception("验证码错误,请重新输入");
                }
                LoginInputDto inputDto = new LoginInputDto(username, Md5.GetMD5(password));
                UserDto       userDto  = _sysUser.CheckLogin(inputDto);
                if (userDto != null)
                {
                    UserInfo info = new UserInfo();
                    info.UserID   = userDto.Id;
                    info.UserName = userDto.UserName;
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket
                                                               (1, JsonConvert.SerializeObject(info), DateTime.Now, DateTime.Now.AddMinutes(20), true, "role");
                    string     encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie authCookie      = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
                }
                return(Json(new AjaxResult {
                    state = ResultType.success.ToString(), message = "登录成功。"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new AjaxResult {
                    state = ResultType.error.ToString(), message = ex.Message
                }));
            }
        }