public async Task <IActionResult> LocalLogin(LocalAuthenticationViewModel localAuthenticationViewModel) { var authenticatedUser = await SetUser(); if (authenticatedUser != null && authenticatedUser.Identity != null && authenticatedUser.Identity.IsAuthenticated) { return(RedirectToAction("Index", "User", new { area = "UserManagement" })); } if (localAuthenticationViewModel == null) { throw new ArgumentNullException(nameof(localAuthenticationViewModel)); } if (ModelState.IsValid) { ResourceOwner resourceOwner = null; try { resourceOwner = await _smsAuthenticationOperation.Execute(localAuthenticationViewModel.PhoneNumber); } catch (Exception ex) { _simpleIdentityServerEventSource.Failure(ex.Message); ModelState.AddModelError("message_error", ex.Message); } if (resourceOwner != null) { var claims = resourceOwner.Claims; claims.Add(new Claim(ClaimTypes.AuthenticationInstant, DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer)); await SetPasswordLessCookie(claims); try { return(RedirectToAction("ConfirmCode")); } catch (Exception ex) { _simpleIdentityServerEventSource.Failure(ex.Message); ModelState.AddModelError("message_error", "TWILIO account is not valid"); } } } var viewModel = new AuthorizeViewModel(); await SetIdProviders(viewModel); await TranslateView(DefaultLanguage); return(View("Index", viewModel)); }
public async Task <ActionResult> LocalLogin(LocalAuthenticationViewModel authorizeViewModel) { var authenticatedUser = await SetUser(); if (authenticatedUser != null && authenticatedUser.Identity != null && authenticatedUser.Identity.IsAuthenticated) { return(RedirectToAction("Index", "User", new { area = "UserManagement" })); } if (authorizeViewModel == null) { throw new ArgumentNullException(nameof(authorizeViewModel)); } if (!ModelState.IsValid) { await TranslateView(DefaultLanguage); var viewModel = new AuthorizeViewModel(); await SetIdProviders(viewModel); return(View("Index", viewModel)); } try { var resourceOwner = await _resourceOwnerAuthenticateHelper.Authenticate(authorizeViewModel.Login, authorizeViewModel.Password, new[] { Constants.AMR }); if (resourceOwner == null) { throw new IdentityServerAuthenticationException("the resource owner credentials are not correct"); } var claims = resourceOwner.Claims; claims.Add(new Claim(ClaimTypes.AuthenticationInstant, DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer)); var subject = claims.First(c => c.Type == Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject).Value; if (string.IsNullOrWhiteSpace(resourceOwner.TwoFactorAuthentication)) { await SetLocalCookie(claims, Guid.NewGuid().ToString()); _simpleIdentityServerEventSource.AuthenticateResourceOwner(subject); return(RedirectToAction("Index", "User", new { area = "UserManagement" })); } // 2.1 Store temporary information in cookie await SetTwoFactorCookie(claims); // 2.2. Send confirmation code try { var code = await _authenticateActions.GenerateAndSendCode(subject); _simpleIdentityServerEventSource.GetConfirmationCode(code); return(RedirectToAction("SendCode")); } catch (ClaimRequiredException) { return(RedirectToAction("SendCode")); } catch (Exception) { throw new Exception("Two factor authenticator is not properly configured"); } } catch (Exception exception) { _simpleIdentityServerEventSource.Failure(exception.Message); await TranslateView(DefaultLanguage); ModelState.AddModelError("invalid_credentials", exception.Message); var viewModel = new AuthorizeViewModel(); await SetIdProviders(viewModel); return(View("Index", viewModel)); } }
public async Task <IActionResult> LocalLogin( [FromForm] LocalAuthenticationViewModel authorizeViewModel, CancellationToken cancellationToken) { if (authorizeViewModel.Login == null || authorizeViewModel.Password == null) { BadRequest(); } var authenticatedUser = await SetUser().ConfigureAwait(false); if (authenticatedUser?.Identity != null && authenticatedUser.Identity.IsAuthenticated) { return(RedirectToAction("Index", "User")); } if (!ModelState.IsValid) { var viewModel = new AuthorizeViewModel(); await SetIdProviders(viewModel).ConfigureAwait(false); RouteData.Values["view"] = "Index"; return(Ok(viewModel)); } try { var resourceOwner = await _resourceOwnerServices.Authenticate( authorizeViewModel !.Login !, authorizeViewModel !.Password !, cancellationToken) .ConfigureAwait(false); if (resourceOwner == null) { _logger.LogError(Strings.TheResourceOwnerCredentialsAreNotCorrect); var viewModel = new AuthorizeViewModel { Password = authorizeViewModel.Password, UserName = authorizeViewModel.Login }; await SetIdProviders(viewModel).ConfigureAwait(false); RouteData.Values["view"] = "Index"; return(Ok(viewModel)); } resourceOwner.Claims = resourceOwner.Claims.Add( new Claim( ClaimTypes.AuthenticationInstant, DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer)); var subject = resourceOwner.Claims.First(c => c.Type == OpenIdClaimTypes.Subject).Value; if (string.IsNullOrWhiteSpace(resourceOwner.TwoFactorAuthentication)) { await SetLocalCookie(resourceOwner.Claims, Id.Create()).ConfigureAwait(false); return(!string.IsNullOrWhiteSpace(authorizeViewModel.ReturnUrl) ? Redirect(authorizeViewModel.ReturnUrl) : RedirectToAction("Index", "User")); } // 2.1 Store temporary information in cookie await SetTwoFactorCookie(resourceOwner.Claims).ConfigureAwait(false); // 2.2. Send confirmation code try { await SendCode(subject, cancellationToken).ConfigureAwait(false); return(RedirectToAction("SendCode")); } catch (ClaimRequiredException cre) { await _eventPublisher.Publish( new SimpleAuthError( Id.Create(), cre.Code, cre.Message, string.Empty, DateTimeOffset.UtcNow)) .ConfigureAwait(false); return(RedirectToAction("SendCode")); } catch (Exception ex) { await _eventPublisher.Publish( new SimpleAuthError( Id.Create(), "misconfigured_2fa", ex.Message, string.Empty, DateTimeOffset.UtcNow)) .ConfigureAwait(false); throw new Exception(Strings.TwoFactorNotProperlyConfigured, ex); } } catch (Exception exception) { await _eventPublisher.Publish( new SimpleAuthError( Id.Create(), InvalidCredentials, exception.Message, string.Empty, DateTimeOffset.UtcNow)) .ConfigureAwait(false); ModelState.AddModelError(InvalidCredentials, exception.Message); var viewModel = new AuthorizeViewModel(); await SetIdProviders(viewModel).ConfigureAwait(false); RouteData.Values["view"] = "Index"; return(Ok(viewModel)); } }