private async Task <bool> UpdateEngineer(ServiceEngineerViewModel serviceEngineer)
        {
            // Alterar usuario
            ApplicationUser user = await _userManager.FindByEmailAsync(serviceEngineer.Registration.Email);

            user.UserName = serviceEngineer.Registration.UserName;
            IdentityResult result = await _userManager.UpdateAsync(user);

            if (result.Succeeded == false)
            {
                result.Errors.ToList().ForEach((error) => ModelState.AddModelError(string.Empty, error.Description));
                return(result.Succeeded);
            }

            // Alterar senha
            string token = await _userManager.GeneratePasswordResetTokenAsync(user);

            IdentityResult passwordResult = await _userManager.ResetPasswordAsync(user, token, serviceEngineer.Registration.Password);

            if (passwordResult.Succeeded == false)
            {
                passwordResult.Errors.ToList().ForEach((error) => ModelState.AddModelError(string.Empty, error.Description));
                return(passwordResult.Succeeded);
            }

            // Alterar claims
            user = await _userManager.FindByEmailAsync(serviceEngineer.Registration.Email);

            MongoClaim     isActiveClaim     = user.Claims.SingleOrDefault(p => p.Type == "IsActive");
            IdentityResult removeClaimResult = await _userManager.RemoveClaimAsync(user, new Claim(isActiveClaim.Type, isActiveClaim.Value));

            IdentityResult addClaimResult = await _userManager.AddClaimAsync(user, new Claim(isActiveClaim.Type, serviceEngineer.Registration.IsActive.ToString()));

            return(true);
        }
        private async Task <bool> CreateEngineer(ServiceEngineerViewModel serviceEngineer)
        {
            // Criar usuario
            var user = new ApplicationUser
            {
                UserName       = serviceEngineer.Registration.UserName,
                Email          = serviceEngineer.Registration.Email,
                EmailConfirmed = true
            };

            IdentityResult result = await _userManager.CreateAsync(user, serviceEngineer.Registration.Password);

            if (result.Succeeded)
            {
                await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Email, serviceEngineer.Registration.Email));

                await _userManager.AddClaimAsync(user, new Claim("IsActive", serviceEngineer.Registration.IsActive.ToString()));

                // Assign user to Engineer Role
                IdentityResult roleResult = await _userManager.AddToRoleAsync(user, Roles.Engineer.ToString());

                if (roleResult.Succeeded == false)
                {
                    roleResult.Errors.ToList().ForEach(p => ModelState.AddModelError("", p.Description));
                    return(roleResult.Succeeded);
                }

                return(result.Succeeded);
            }
            else
            {
                result.Errors.ToList().ForEach(p => ModelState.AddModelError("", p.Description));
                return(result.Succeeded);
            }
        }
 private async Task SendEmailToUser(ServiceEngineerViewModel serviceEngineer)
 {
     if (serviceEngineer.Registration.IsActive)
     {
         await _emailSender.SendEmailAsync(serviceEngineer.Registration.Email,
                                           "Account Created/Modified",
                                           $"Email : {serviceEngineer.Registration.Email} /n Passowrd : {serviceEngineer.Registration.Password}            ");
     }
     else
     {
         await _emailSender.SendEmailAsync(serviceEngineer.Registration.Email,
                                           "Account Deactivated",
                                           "Your account has been deactivated.");
     }
 }
        public async Task <IActionResult> ServiceEngineers()
        {
            var serviceEngineers = await _userManager.GetUsersInRoleAsync(Roles.Engineer.ToString());

            // Mantenha todos os engenheiros de serviço na sessão
            HttpContext.Session.SetSession("ServiceEngineers", serviceEngineers);

            var engineersViewModel = new ServiceEngineerViewModel
            {
                Registration = new ServiceEngineerRegistrationViewModel()
                {
                    IsEdit = false
                },
                ServiceEngineers = serviceEngineers?.ToList()
            };

            return(View(engineersViewModel));
        }
        public async Task <IActionResult> ServiceEngineers(ServiceEngineerViewModel serviceEngineer)
        {
            serviceEngineer.ServiceEngineers = HttpContext.Session.GetSession <List <ApplicationUser> >("ServiceEngineers");

            if (ModelState.IsValid == false)
            {
                return(View(serviceEngineer));
            }

            // If the IsEdit flag is false, a new user is created with the given details and password and is added to the Engineer role.
            bool success = serviceEngineer.Registration.IsEdit
                ? await UpdateEngineer(serviceEngineer)
                : await CreateEngineer(serviceEngineer);

            // Finally, an e-mail is sent to the user with e-mail and password details used to create/modify the account.
            // await SendEmailToUser(serviceEngineer);

            return((success == true)
                ? (IActionResult)RedirectToAction("ServiceEngineers")
                : View(serviceEngineer));
        }
        public async Task <IActionResult> ServiceEngineers(ServiceEngineerViewModel serviceEngineer)
        {
            serviceEngineer.ServiceEngineers = HttpContext.Session.GetSession <List <ApplicationUser> >("ServiceEngineers");
            if (!ModelState.IsValid)
            {
                return(View(serviceEngineer));
            }

            if (serviceEngineer.Registration.IsEdit)
            {
                // Update User
                var user = await _userManager.FindByEmailAsync(serviceEngineer.Registration.Email);

                user.UserName = serviceEngineer.Registration.UserName;
                IdentityResult result = await _userManager.UpdateAsync(user);

                if (!result.Succeeded)
                {
                    result.Errors.ToList().ForEach(p => ModelState.AddModelError("", p.Description));
                    return(View(serviceEngineer));
                }

                // Update Password
                var token = await _userManager.GeneratePasswordResetTokenAsync(user);

                IdentityResult passwordResult = await _userManager.ResetPasswordAsync(user, token, serviceEngineer.Registration.Password);

                if (!passwordResult.Succeeded)
                {
                    passwordResult.Errors.ToList().ForEach(p => ModelState.AddModelError("", p.Description));
                    return(View(serviceEngineer));
                }

                // Update claims
                user = await _userManager.FindByEmailAsync(serviceEngineer.Registration.Email);

                var isActiveClaim     = user.Claims.SingleOrDefault(p => p.ClaimType == "IsActive");
                var removeClaimResult = await _userManager.RemoveClaimAsync(user,
                                                                            new System.Security.Claims.Claim(isActiveClaim.ClaimType, isActiveClaim.ClaimValue));

                var addClaimResult = await _userManager.AddClaimAsync(user,
                                                                      new System.Security.Claims.Claim(isActiveClaim.ClaimType, serviceEngineer.Registration.IsActive.ToString()));
            }
            else
            {
                // Create User
                ApplicationUser user = new ApplicationUser
                {
                    UserName       = serviceEngineer.Registration.UserName,
                    Email          = serviceEngineer.Registration.Email,
                    EmailConfirmed = true
                };

                IdentityResult result = await _userManager.CreateAsync(user, serviceEngineer.Registration.Password);

                await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", serviceEngineer.Registration.Email));

                await _userManager.AddClaimAsync(user, new System.Security.Claims.Claim("IsActive", serviceEngineer.Registration.IsActive.ToString()));

                if (!result.Succeeded)
                {
                    result.Errors.ToList().ForEach(p => ModelState.AddModelError("", p.Description));
                    return(View(serviceEngineer));
                }

                // Assign user to Engineer Role
                var roleResult = await _userManager.AddToRoleAsync(user, Roles.Engineer.ToString());

                if (!roleResult.Succeeded)
                {
                    roleResult.Errors.ToList().ForEach(p => ModelState.AddModelError("", p.Description));
                    return(View(serviceEngineer));
                }
            }

            if (serviceEngineer.Registration.IsActive)
            {
                await _emailSender.SendEmailAsync(serviceEngineer.Registration.Email,
                                                  "Account Created/Modified",
                                                  $"Email : {serviceEngineer.Registration.Email} /n Passowrd : {serviceEngineer.Registration.Password}");
            }
            else
            {
                await _emailSender.SendEmailAsync(serviceEngineer.Registration.Email,
                                                  "Account Deactivated",
                                                  $"Your account has been deactivated.");
            }

            return(RedirectToAction("ServiceEngineers"));
        }