public ActionResult Index(UsuarioPedidoViewModel loginViewModel) { if (!ModelState.IsValid) { ModelState.AddModelError("Usuario", "Os dados devem ser preenchidos"); return View(); } var usuario = new UsuarioDto() { Usuario = loginViewModel.Usuario, Senha = loginViewModel.Senha }; bool IsValidUser = usuarioLogic.verificaUsuario(usuario); if ( !IsValidUser ) { ModelState.Clear(); ModelState.AddModelError("Usuario", "Usuário inválido"); ModelState.AddModelError("Senha", "Senha inválida"); return View(); } criaCookie(loginViewModel); return Redirect("/Retrieve"); }
private void criaCookie(UsuarioPedidoViewModel model) { var cookie = FormsAuthentication.GetAuthCookie(model.Usuario, true); var ticket = FormsAuthentication.Decrypt(cookie.Value); var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, model.Usuario, ticket.CookiePath); var encTicket = FormsAuthentication.Encrypt(newTicket); cookie.Value = encTicket; System.Web.HttpContext.Current.Response.Cookies.Add(cookie); }