Esempio n. 1
0
        public ActionResult Index([FromBody] AccountViewModel account)
        {
            try
            {
                var user = new UserModel
                {
                    LoginName = account.UserName,
                    Password  = CommonUtil.Encrypt(account.Password)
                };
                var result = BllLogin.Login(user);
                if (!string.IsNullOrEmpty(result))
                {
                    ViewBag.ErrorInfo = result;
                    Session["User"]   = null;
                    return(View());
                }

                user.TokenKey = Guid.NewGuid().ToString();

                var cookie = FormsAuthentication.GetAuthCookie("Username", false);
                var ticket = FormsAuthentication.Decrypt(cookie.Value);
                if (ticket == null)
                {
                    throw new Exception("无效票据");
                }
                var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate,
                                                              ticket.Expiration, ticket.IsPersistent, JsonConvert.SerializeObject(user));
                cookie.Value = FormsAuthentication.Encrypt(newTicket);
                Response.Cookies.Set(cookie);

                Session["User"] = user;
                return(RedirectToAction("Index", "MapBoard"));
            }
            catch
            {
                return(View());
            }
        }