Ejemplo n.º 1
0
        public ActionResult Index(Usuario model)
        {
            if (ModelState.IsValid) //verifica se é válido
            {
                var Servico = new UsuarioServico();
                try
                {
                    model = Servico.ObterPorUsuario(model.UserName, model.Password);
                    if (model != null)
                    {
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                                                                                         model.UserName,
                                                                                         DateTime.Now,
                                                                                         DateTime.Now.AddDays(30),
                                                                                         true,
                                                                                         model.Id.ToString(),
                                                                                         FormsAuthentication.FormsCookiePath);

                        string encTicket = FormsAuthentication.Encrypt(ticket);
                        Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    return(View(model));
                }
                return(RedirectToAction("index", "Home"));
            }
            else
            {
                return(View(model));
            }
        }