Exemple #1
0
        public ActionResult Register(string username, string password, string passwordRepeat, string email, string emailRepeat)
        {
            if (username.IsNullOrWhiteSpace() || password.IsNullOrWhiteSpace() || passwordRepeat.IsNullOrWhiteSpace() ||
                email.IsNullOrWhiteSpace() || emailRepeat.IsNullOrWhiteSpace() || password != passwordRepeat ||
                email != emailRepeat)
            {
                TempData["Error"] = Utility.GetErrorMessage(Utility.ErrorType.InvalidFormData);
                return(RedirectToAction("Index", "Home"));
            }

            int numOfAccounts = db.UserAccounts.Count(s => s.Username == username);

            if (numOfAccounts != 0)
            {
                TempData["Error"] = Utility.GetErrorMessage(Utility.ErrorType.AccountWithThisUsernameAlreadyExists);
                logger.Info("Account already exists for username " + username);
                return(RedirectToAction("Index", "Home"));
            }
            int numOfEmails = db.UserAccounts.Count(s => s.Email == email);

            if (numOfEmails != 0)
            {
                TempData["Error"] = Utility.GetErrorMessage(Utility.ErrorType.AccountWithThisEmailAlreadyExists);
                logger.Info("Account already exists for email " + email);
                return(RedirectToAction("Index", "Home"));
            }

            string link = null;

            do
            {
                link = (username + DateTime.Now.ToString("U") + (new Random()).Next(0, 1000).ToString()).GetHashCode().ToString();
            } while (db.UserAccounts.Any(x => x.Confirmationlink == link));

            UserAccount newUser = new UserAccount();

            newUser.Username         = username;
            newUser.PasswordSalt     = Utility.GenerateRandomString();
            newUser.Password         = Utility.HashPassword(password, newUser.PasswordSalt);
            newUser.Email            = email;
            newUser.Role             = Utility.AccountTypeToInt(Utility.AccountType.Unconfirmed);
            newUser.Confirmationlink = link;
            newUser.DateCreated      = DateTime.Now;
            newUser.DateModified     = DateTime.Now;
            newUser.LastLogin        = DateTime.Now;
            UrlHelper u = new UrlHelper(this.ControllerContext.RequestContext);

            if (!Utility.SendConfirmationEmail(newUser.Email, u.Action("ConfirmEmail", "AccountOptions", new { confirmationLink = newUser.Confirmationlink })))
            {
                TempData["Error"] = Utility.GetErrorMessage(Utility.ErrorType.ErrorInSendingEmail);
                logger.Warn("Error in sending email confirmation to user " + newUser.Username);
                return(RedirectToAction("Index", "AccountOptions"));
            }

            db.UserAccounts.Add(newUser);
            db.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }
        public void AccountTypeToIntPositiveTest()
        {
            int accountType = Utility.AccountTypeToInt(Utility.AccountType.User);

            Assert.AreEqual(accountType, 0);
        }