public async Task <(bool isSucceed, string message)> LockoutUserAccountAsync(LockoutUserDTO model)
        {
            try
            {
                var(isSucceed, message) = (true, model.IsLocked ? "User lockedout successfully" : "User unlock successfully");

                var user = await _userManager.FindByIdAsync(model.Id);

                if (user == null)
                {
                    throw new Exception("User not found");
                }

                if (user.UserName.ToLower().Equals("rootadmin") || user.UserName == "admin" || user.IsSystemUser)
                {
                    throw new Exception("You can not lockout system user");
                }

                IdentityResult result;

                if (model.IsLocked)
                {
                    result = await _userManager.SetLockoutEnabledAsync(user, true);

                    if (result.Succeeded)
                    {
                        if (model.ForDays.HasValue)
                        {
                            result = await _userManager.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow.AddDays(model.ForDays.Value));
                        }
                        else
                        {
                            result = await _userManager.SetLockoutEndDateAsync(user, DateTimeOffset.MaxValue);
                        }
                    }
                }
                else
                {
                    result = await _userManager.SetLockoutEndDateAsync(user, null);
                }

                if (!result.Succeeded)
                {
                    isSucceed = false;
                    message   = GetIdentityResultMessage(result, "Unknown error occured");
                }

                return(isSucceed, message);
            }
            catch (Exception e)
            {
                return(false, e.Message);
            }
        }
Example #2
0
        public async Task <IActionResult> Lockout([FromBody] LockoutUserDTO model)
        {
            var(isSucceeded, message) = await _userRepository.LockoutUserAccountAsync(model);

            return(Ok((isSucceeded, message)));
        }