Exemplo n.º 1
0
        public ActionResult SignIn(string login, string password, bool?rememberMe, string returnUrl)
        {
            // TODO : Nettoyage des traces de cette fonction lors de la résolution de l'anomalie #248

            Debug($"Post ~/Administration/User/SignIn({login}, {password}, {rememberMe}, {returnUrl})");

            // check the value by itself

            Errors errors = new Errors();

            if (string.IsNullOrWhiteSpace(login) || string.IsNullOrWhiteSpace(password))
            {
                Debug($"Login '{login}' or password '{password}' not defined !");

                // no login set

                if (string.IsNullOrWhiteSpace(login))
                {
                    errors.AddField("Login", "ERR_FIELD_REQUIRED", new object[] { "{USER_LOGIN}" });
                }

                // no password set

                if (string.IsNullOrWhiteSpace(password))
                {
                    errors.AddField("Password", "ERR_FIELD_REQUIRED", new object[] { "{USER_PASSWORD}" });
                }
            }
            else if (StatusManager.Status == StatusManager.EStatus.STATUS_OK)
            {
                Debug($"Status '{StatusManager.Status}' is OK !");

                // authentication of the user

                UserRecord userAuthenticated = _userManager.Authenticate(login, password);

                if (userAuthenticated != null)
                {
                    FormsAuthentication.SetAuthCookie(userAuthenticated.Id.ToString(), (rememberMe != null && rememberMe.Value));

                    Debug("Authentication OK");

                    if (!string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
                    {
                        return(Redirect(returnUrl));
                    }

                    return(RedirectToAction("Index"));
                }

                errors.AddGlobal("ERR_LOGIN_INCORRECT");
            }
            else
            {
                Debug($"Status '{StatusManager.Status}' is not OK !");

                // it is due to a upgrading process ... no error because StatusFilterAttribute has already rejected the action

                UserRecord administrator = UserRecord.CreateDefaultAdministrator();

                if (administrator.Login.Equals(login) && administrator.Password.Equals(password))
                {
                    FormsAuthentication.SetAuthCookie(administrator.Id.ToString(), false);
                    return(View("Upgrading", new UserViewModel(new LanguageDictionary(Server.MapPath(LanguageDictionary.DIRECTORY_IMAGE), ConfigurationManager.DefaultLanguage),
                                                               administrator,
                                                               true)));
                }

                errors.AddGlobal("ERR_LOGIN_INCORRECT");
            }

            Debug("Authentication fail");

            // load multilingual dictionary

            LanguageDictionary ressources = new LanguageDictionary(Server.MapPath(LanguageDictionary.DIRECTORY_IMAGE), ConfigurationManager.DefaultLanguage);

            if (StatusManager.Status == StatusManager.EStatus.STATUS_OK)
            {
                using (DatabaseContext database = new DatabaseContext())
                    ressources.Load(database, 1);
            }

            // update ModelState on depends on errors

            SetModelState(ModelState, ressources, errors);

            // show the same screen until the user success

            if (StatusManager.Status == StatusManager.EStatus.STATUS_UPGRADING)
            {
                return(View("SignInAdministration", new UserViewModel(ressources,
                                                                      new UserRecord {
                    Login = login, Password = password
                },
                                                                      false,
                                                                      null,
                                                                      rememberMe: rememberMe != null && rememberMe.Value)));
            }

            return(View(new UserViewModel(ressources,
                                          new UserRecord {
                Login = login, Password = password
            },
                                          false,
                                          null,
                                          rememberMe: rememberMe != null && rememberMe.Value)));
        }
Exemplo n.º 2
0
        public ActionResult NewPassword(string key, string login, string newPassword1, string newPassword2)
        {
            Debug($"Post ~/Administration/User/NewPassword({login}, {key})");

            bool loginIncorrect = false;

            // read the multilingual dictionary

            LanguageDictionary ressources = new LanguageDictionary(Server.MapPath(LanguageDictionary.DIRECTORY_IMAGE), ConfigurationManager.DefaultLanguage);

            ressources.Load(_userManager.Database, 1);

            // check the value by itself

            Errors errors = new Errors();

            if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(login))
            {
                errors.AddField("Login", "ERR_FIELD_REQUIRED", new object[] { "{USER_LOGIN}" });
            }
            else
            {
                // Check if the login exists ...

                UserRecord user = _userManager.GetByLogin(login, false);
                if (user == null || String.IsNullOrEmpty(user.NewPasswordKey) || !user.NewPasswordKey.Equals(key))
                {
                    loginIncorrect = true;
                    errors.AddGlobal("ERR_LOGIN_INCORRECT");
                }
            }

            if (string.IsNullOrWhiteSpace(newPassword1))
            {
                errors.AddField("NewPassword1", "ERR_FIELD_REQUIRED", new object[] { "{USER_NEW_PASSWORD}" });
            }

            if (string.IsNullOrWhiteSpace(newPassword2))
            {
                errors.AddField("NewPassword2", "ERR_FIELD_REQUIRED", new object[] { "{USER_RETYPE_PASSWORD}" });
            }

            if (!loginIncorrect &&
                !string.IsNullOrWhiteSpace(newPassword1) &&
                !string.IsNullOrWhiteSpace(newPassword2) &&
                !newPassword1.Equals(newPassword2))
            {
                errors.AddGlobal("ERR_LOGIN_INCORRECT");
            }

            if (errors.HasError)
            {
                SetModelState(ModelState, ressources, errors);
                return(View(new UserViewModel(ressources, new UserRecord(), false)));
            }

            // Update the password

            try
            {
                _userManager.SetNewPassword(login, newPassword1);
            }
            catch (System.Exception ex)
            {
                Error($"An exception occurs on updating the password: {ex.Message}");
            }

            // Sign out the user and resign the user

            FormsAuthentication.SignOut();
            return(RedirectToAction("SignIn"));
        }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor within a message
 /// </summary>
 /// <param name="message"></param>
 /// <param name="parameters"></param>
 public ExceptionDefinitionRecord(string message, params object[] parameters) : base(message)
 {
     Errors = new Errors();
     Errors.AddGlobal(message, parameters);
 }