public string EncryptPassword(string password)
        {
            HashString hash         = new HashString();
            string     hashPassword = hash.ComputeSha256Hash(password);

            return(hashPassword);
        }
Exemple #2
0
        public async Task <IActionResult> OnPostSaveEditAsync()
        {
            ApplicationUser.EmailAddress = EmailAddress;
            ApplicationUser.FirstName    = FirstName;
            ApplicationUser.LastName     = LastName;

            HashString Hash = new HashString();

            CurrentPassword = Hash.ComputeSha256Hash(CurrentPassword);

            if (!CurrentPassword.Equals(Password))
            {
                CurrentPasswordErrorMessage = "Current password mismatch!!";
                return(Page());
            }

            ApplicationUser.Password = Hash.ComputeSha256Hash(Password);

            _context.Attach(ApplicationUser).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ApplicationUserExists(ApplicationUser.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            EmailAddress = "";
            FirstName    = "";
            LastName     = "";
            Password     = "";

            return(RedirectToPage("/AppUser/Profile", new { Id = ApplicationUser.ID }));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            ConfirmPasswordErrorMessage = "";
            EmailAddressErrorMessage    = "";

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if (!ConfirmPassword.Equals(ApplicationUser.Password))
            {
                ConfirmPasswordErrorMessage = "Passwords do not match!!";
                return(Page());
            }

            try
            {
                // The Contains method is run on the database
                var email = (from u in _context.ApplicationUser
                             where (u.EmailAddress == ApplicationUser.EmailAddress)
                             select u.EmailAddress).Single();

                EmailAddressErrorMessage = "An account with this Email Address already exists!!";
                return(Page());
            }
            catch (InvalidOperationException) //this error happens if the email address is unique
            {
                HashString Hash = new HashString();
                ApplicationUser.Password = Hash.ComputeSha256Hash(ApplicationUser.Password);

                _context.ApplicationUser.Add(ApplicationUser);
                await _context.SaveChangesAsync();

                await this.SignInUser(ApplicationUser.EmailAddress, false);

                return(RedirectToPage("/AppUser/Profile", new { Id = ApplicationUser.ID }));
            }
        }
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // Checks if the email and password match
            HashString Hash = new HashString();

            Input.Password = Hash.ComputeSha256Hash(Input.Password);

            var AppUser = _context.ApplicationUser
                          .Where(u => u.EmailAddress == Input.Email && u.Password == Input.Password)
                          .FirstOrDefault();

            if (AppUser == null)
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return(Page());
            }

            if (RememberEmailAddress == true)
            {
                var cookieOptions = new CookieOptions
                {
                    Expires = DateTime.Now.AddDays(30)
                };

                // saving the email addressin a cookie and when it expires
                Response.Cookies.Append("EmailAddress", Input.Email, cookieOptions);
            }

            await this.SignInUser(Input.Email, false);

            return(RedirectToPage("/AppUser/Profile", new { Id = AppUser.ID }));
        }