コード例 #1
0
        public async Task <IActionResult> ChangePassword(ChangePassword changePassword) // Change password
        {
            if (ModelState.IsValid)
            {
                SomeData someData = null;

                try
                {
                    string someEmail = new HashConvertor().GetHash(changePassword.Email.Trim().ToLower());
                    someData = await _context.SomeDatas.LastOrDefaultAsync(sd => sd.Data1 == someEmail);
                }
                catch (Exception)
                {
                    return(NotFound());
                }

                if (someData == null)
                {
                    return(RedirectToAction("Register", "Account"));
                }

                Cryptographer cryptographer = new Cryptographer().Create(someData.Data2);

                string email = cryptographer.Encode(changePassword.Email.Trim().ToLower());

                string passwordHash = new HashConvertor().GetHash(changePassword.Password.Trim().ToLower());

                bool responce = false;

                try
                {
                    await Task.Run(() =>
                    {
                        responce = new UserUtils().CheckEmailExists(_context, email);
                    });
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("Error", "Account"));
                }

                if (!responce)
                {
                    return(RedirectToAction("Register", "Account"));
                }

                UserApp userApp = null;
                try
                {
                    userApp = await _context.UserApps.SingleOrDefaultAsync(u => u.Email == email);

                    if (userApp != null)
                    {
                        userApp = cryptographer.ConvertUserData(userApp, passwordHash);

                        try
                        {
                            _context.Update(userApp);
                            await _context.SaveChangesAsync();
                        }
                        catch (Exception)
                        {
                            return(NotFound());
                        }

                        await Task.Run(() =>
                        {
                            userApp = new UserUtils().UpdatePassword(_context, userApp.Email, passwordHash);
                        });

                        string emailHash = new HashConvertor().GetHash(changePassword.Email.Trim().ToLower());

                        _context.Add(new SomeData
                        {
                            Data1 = emailHash,
                            Data2 = passwordHash
                        });
                        await _context.SaveChangesAsync();
                    }
                }
                catch (Exception)
                {
                    HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later.");
                    return(RedirectToAction("Error", "Account"));
                }

                if (userApp != null)
                {
                    int role;

                    try
                    {
                        role = _context.UserRoles.Where(r => r.Id == userApp.IdRole).Select(r => r.IdRole).First();
                    }
                    catch (Exception)
                    {
                        HttpContext.Session.SetString(ERROR, "The server was not found or was not accessible. Try later. I am ");
                        return(RedirectToAction("Error", "Account"));
                    }

                    SetUserSession(userApp, role, cryptographer);

                    return(RedirectToAction("Index", "BoardTasks"));
                }
            }

            return(View());
        }