public ActionResult Login(AuthViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = _authService.Authenticate(model.Email, SecurityUtil.EncryptPassword(model.Password));
                if (user != null)
                {
                    if (!user.Status)
                    {
                        ViewBag.Error = Resources.ErrorMessages.UserInactive;
                        return(View(model));
                    }
                    user.LastLoginAt = DateTime.Now;
                    _userService.Update(user);

                    //Permissions by role
                    List <Permission> permissionsUser = user.Role.Permissions.Select(p => new Permission
                    {
                        Action     = p.Action,
                        Controller = p.Controller,
                        Module     = p.Module
                    }).ToList();

                    //IF SUPPORT, SET ALL PERMISSIONS
                    if (user.Role.Code == Constants.ROLE_IT_SUPPORT)
                    {
                        permissionsUser = _permissionService.GetAll().Select(p => new Permission
                        {
                            Action     = p.Action,
                            Controller = p.Controller,
                            Module     = p.Module
                        }).ToList();
                    }

                    AuthUser authUser = new AuthUser
                    {
                        Id                 = user.Id,
                        FirstName          = user.FirstName,
                        LastName           = user.LastName,
                        Email              = user.Email,
                        Language           = user.Language,
                        Uuid               = user.Uuid,
                        PasswordExpiration = user.PasswordExpiration,
                        Role               = new Role
                        {
                            Code = user.Role.Code,
                            Name = user.Role.Name
                        },
                        Permissions = permissionsUser
                    };

                    //Set user in sesion
                    Authenticator.StoreAuthenticatedUser(authUser);

                    //Set Language
                    LanguageMngr.SetDefaultLanguage();
                    if (!string.IsNullOrEmpty(authUser.Language))
                    {
                        LanguageMngr.SetLanguage(authUser.Language);
                    }

                    if (user.PasswordExpiration.HasValue)
                    {
                        DateTime passwordExpiration = user.PasswordExpiration.Value;
                        DateTime todayDate          = DateUtil.GetDateTimeNow();
                        if (user.PasswordExpiration.Value.Date < todayDate.Date)
                        {
                            return(RedirectToAction("ChangePassword", "Auth"));
                        }
                        string daysBeforeExpireToNotifyConfig = ConfigurationManager.AppSettings["DaysBeforeExpireToNotify"];
                        int    daysBeforeExpireToNotify       = 0;
                        if (Int32.TryParse(daysBeforeExpireToNotifyConfig, out daysBeforeExpireToNotify))
                        {
                            int daysLeft = ((TimeSpan)(passwordExpiration.Date - todayDate.Date)).Days + 1;
                            if (daysLeft <= daysBeforeExpireToNotify)
                            {
                                string message = String.Format(ViewLabels.PASSWORD_EXPIRATION_MESSAGE, daysLeft);
                                AddViewMessage(TypeMessageView.WARNING, message);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(Request.Form["ReturnUrl"]))
                    {
                        return(Redirect(Request.Form["ReturnUrl"]));
                    }
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    AddViewMessage(TypeMessageView.ERROR, ErrorMessages.UserNotExistsOrPasswordInvalid);
                }
            }

            return(View(model));
        }
 //<summary>Recibe la peticion del servidor antes de que llegue al controlador para iniciar el lenguage de la aplicacion.</summary>
 /// <param name="cancellationToken"></param>
 /// <param name="request">Objeto que contiene informacion de las peticiones.</param>
 /// <returns>Respuesta del servidor</returns>
 protected override async System.Threading.Tasks.Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
 {
     LanguageMngr.SetDefaultLanguage();
     return(await base.SendAsync(request, cancellationToken));
 }