Beispiel #1
0
        public async Task <object> LoginAsync(LoginViewModel loginViewModel)
        {
            var result = await SignInManager.PasswordSignInAsync(loginViewModel.Email, loginViewModel.Password, loginViewModel.RememberMe, lockoutOnFailure : false);

            if (result.Succeeded)
            {
                var identity = await GetClaimsIdentity(loginViewModel.Email);

                if (identity == null)
                {
                    throw new InternalServerErrorHttpException("Fehler beim Ausstellen des JWT.");
                }
                var jwt = await JwtTokens.GenerateJwt(identity, JwtFactory, loginViewModel.Email, JwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented });

                return(jwt);
            }
            else
            {
                if (result.IsLockedOut)
                {
                    throw new AccountLockedHttpException("Account wurde gesperrt, weil das Passwort zu oft falsch eingegeben wurde. Bitte beim Support melden.");
                }
                else
                {
                    throw new InvalidEmailAndPasswordCombinationHttpException();
                }
            }
        }
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            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(model.Email, model.Password, model.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User logged in.");
                    //return RedirectToLocal(returnUrl);
                    var identity = await GetClaimsIdentity(model.Email, model.Password);

                    if (identity == null)
                    {
                        ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                        return(BadRequest());
                    }

                    var jwt = await JwtTokens.GenerateJwt(identity, _jwtFactory, model.Email, _jwtOptions, new JsonSerializerSettings { Formatting = Formatting.Indented });

                    ApplicationUser usr = await _userManager.FindByNameAsync(model.Email);

                    var jwtObj = JsonConvert.DeserializeObject <Jwt>(jwt);
                    usr.accessToken = jwtObj.auth_token;
                    await _userManager.UpdateAsync(usr);

                    return(RedirectToLocal(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("User account locked out.");
                    return(RedirectToAction(nameof(Lockout)));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                    return(View(model));
                }
            }

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