コード例 #1
0
        public async Task <bool> Delete(int id)
        {
            var comment = await _context.Comments.FindAsync(id);

            _context.Comments.Remove(comment);
            _logger.Warning("Comment {message} deleted, Id: {id}", comment.Message, comment.Id);
            return(await _context.SaveChangesAsync() > 0);
        }
コード例 #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true

                var user = await _userManager.FindByNameAsync(Input.Email);

                if (user != null && await _userManager.CheckPasswordAsync(user, Input.Password))
                {
                    GenericResult <string> resultJwt = await _jwtService.GetJwt(Input.Email, Input.Password);

                    if (resultJwt.Success)
                    {
                        // Si el usuario tiene el claim de JWT lo elimino y agrego uno nuevo
                        var claimJwt = _context.UserClaims.
                                       Where(x => x.ClaimType == "JWT" && x.UserId == user.Id).FirstOrDefault();

                        if (claimJwt != null)
                        {
                            _context.UserClaims.Remove(claimJwt);
                            await _context.SaveChangesAsync();
                        }
                        await _userManager.AddClaimAsync(user, new Claim("JWT", resultJwt.value));
                    }
                }


                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.Information("{User} logged in.", user.Email);
                    return(LocalRedirect(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.Warning("{User} account locked out.", user.Email);
                    return(RedirectToPage("./Lockout"));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Usuario o contraseña incorrectos.");
                    return(Page());
                }
            }

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