コード例 #1
0
        public async Task <IActionResult> InsertCommentAsync([FromBody] CreateCommentDto model)
        {
            if (!await recaptchaManager.ValidateReCaptchaResponseAsync(model.CaptchaResponse))
            {
                return(Ok());
            }

            await commentService.InsertCommentAsync(model);

            return(Ok());
        }
コード例 #2
0
        private async Task <LoginResponseDto> RunAllAuthenticationChecksAsync(LoginCredentialsDto credentials)
        {
            if (!await recaptchaManager.ValidateReCaptchaResponseAsync(credentials.CaptchaResponse))
            {
                return(LoginResponseDto.Failed());
            }

            if (!BlogConfiguration.EnableLogins)
            {
                logger.LogDebug($"Logins are disabled");
                return(LoginResponseDto.Failed());
            }

            if (credentials.Type == LoginCredentialsType.UsernamePassword)
            {
                logger.LogDebug($"authenticating by username and password");

                var canAuthenticate = await TryAuthenticateUser(credentials);

                if (!canAuthenticate)
                {
                    return(LoginResponseDto.Failed());
                }

                if (BlogConfiguration.EnableTwoFactorAuth)
                {
                    logger.LogDebug($"generating and sending two factor token");

                    var session = Guid.NewGuid().ToString();
                    await twoFactorAuthenticator.SendAndGenerateTwoFactorTokenAsync(session, credentials.Key);

                    return(new LoginResponseDto
                    {
                        Type = LoginResponseType.TwoFactorToken,
                        Success = true,
                        Value = session
                    });
                }
                else
                {
                    logger.LogDebug($"generating and returning auth token");

                    var token = GenerateAuthenticationToken(credentials.Key);
                    return(new LoginResponseDto
                    {
                        Type = LoginResponseType.AuthenticationToken,
                        Success = true,
                        Value = token
                    });
                }
            }
            else if (credentials.Type == LoginCredentialsType.TwoFactor)
            {
                logger.LogDebug($"authenticating by two factor token");

                if (await twoFactorAuthenticator.TryAuthenticateSecondFactor(credentials.Session, credentials.Key, credentials.Secret))
                {
                    logger.LogDebug($"generating and returning auth token");

                    var token = GenerateAuthenticationToken(credentials.Key);
                    return(new LoginResponseDto
                    {
                        Type = LoginResponseType.AuthenticationToken,
                        Success = true,
                        Value = token
                    });
                }
            }
            return(LoginResponseDto.Failed());
        }