コード例 #1
0
        public async Task <ActionResult> EditAsync(Moderator moderator)
        {
            if (ModelState.IsValid)
            {
                var user = _userService.Find(moderator.UserId);
                if (user == null)
                {
                    return(NotFound());
                }

                var isModerator = await _userService.IsUserInRole(user, "Moderator");

                IdentityResult result = null;

                if (isModerator && !moderator.IsActive)
                {
                    result = await _userService.DeleteRoleFromUserAsync(user, "Moderator");
                }
                ;
                if (!isModerator && moderator.IsActive)
                {
                    result = await _userService.AddRoleToUserAsync(user, "Moderator");
                }
                _moderatorService.Update(moderator);
                return(RedirectToAction("Manage"));
            }
            return(View(moderator));
        }
コード例 #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                //var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
                var user = await _userManager.FindByEmailAsync(Input.Email);

                if (await _userManager.CheckPasswordAsync(user, Input.Password) == false)
                {
                    ModelState.AddModelError("message", "Login lub hasło są nieprawidłowe.");
                    return(Page());
                }
                if (user.IsActive == false)
                {
                    ModelState.AddModelError("message", "To konto jest niaktywne.");
                    return(Page());
                }
                var result = await _signInManager.PasswordSignInAsync(user.UserName, Input.Password, Input.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    var moderator = _moderatorService.Find(user.Id);
                    if (moderator != null)
                    {
                        moderator.LastLoginDate = DateTime.Now;
                        _moderatorService.Update(moderator);
                    }
                    return(LocalRedirect(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(Page());
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }