|
|
@ -81,7 +81,7 @@ public class AuthController : ApiController |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status400BadRequest)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status429TooManyRequests)] |
|
|
|
public async Task<ActionResult<OperationResult<AuthenticateUserResponse>>> Login([FromBody] AuthenticateUserCommand command) |
|
|
|
public async Task<OperationResult<AuthenticateUserResponse>> Login([FromBody] AuthenticateUserCommand command) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
@ -92,8 +92,7 @@ public class AuthController : ApiController |
|
|
|
if (attempts >= _authConfig.MaxLoginAttempts) |
|
|
|
{ |
|
|
|
_logger.LogWarning("账号 {UserName} 登录尝试次数过多", command.UserName); |
|
|
|
return StatusCode(StatusCodes.Status429TooManyRequests, |
|
|
|
OperationResult<AuthenticateUserResponse>.CreateFailure("登录尝试次数过多,请稍后再试")); |
|
|
|
return OperationResult<AuthenticateUserResponse>.CreateFailure("登录尝试次数过多,请稍后再试"); |
|
|
|
} |
|
|
|
|
|
|
|
// 执行登录
|
|
|
@ -123,13 +122,12 @@ public class AuthController : ApiController |
|
|
|
_logger.LogWarning($"Bearer {result.Data.AccessToken}"); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "账号 {UserName} 登录时发生异常", command.UserName); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<AuthenticateUserResponse>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<AuthenticateUserResponse>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -156,10 +154,10 @@ public class AuthController : ApiController |
|
|
|
/// <response code="400">登录失败,返回错误信息</response>
|
|
|
|
/// <response code="429">登录尝试次数过多</response>
|
|
|
|
[HttpPost("email")] |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status400BadRequest)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status429TooManyRequests)] |
|
|
|
public async Task<ActionResult<OperationResult<AuthenticateUserResponse>>> LoginWithEmail([FromBody] EmailLoginCommand command) |
|
|
|
[ProducesResponseType(typeof(OperationResult<EmailLoginResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<EmailLoginResponse>), StatusCodes.Status400BadRequest)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<EmailLoginResponse>), StatusCodes.Status429TooManyRequests)] |
|
|
|
public async Task<OperationResult<EmailLoginResponse>> LoginWithEmail([FromBody] EmailLoginCommand command) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
@ -170,8 +168,7 @@ public class AuthController : ApiController |
|
|
|
if (attempts >= _authConfig.MaxLoginAttempts) |
|
|
|
{ |
|
|
|
_logger.LogWarning("邮箱 {Email} 登录尝试次数过多", command.Email); |
|
|
|
return StatusCode(StatusCodes.Status429TooManyRequests, |
|
|
|
OperationResult<AuthenticateUserResponse>.CreateFailure("登录尝试次数过多,请稍后再试")); |
|
|
|
return OperationResult<EmailLoginResponse>.CreateFailure("登录尝试次数过多,请稍后再试"); |
|
|
|
} |
|
|
|
|
|
|
|
// 执行登录
|
|
|
@ -201,13 +198,12 @@ public class AuthController : ApiController |
|
|
|
_logger.LogWarning($"Bearer {result.Data.AccessToken}"); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "邮箱 {Email} 登录时发生异常", command.Email); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<AuthenticateUserResponse>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<EmailLoginResponse>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -238,7 +234,7 @@ public class AuthController : ApiController |
|
|
|
[HttpPost("register")] |
|
|
|
[ProducesResponseType(typeof(OperationResult<RegisterUserResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<RegisterUserResponse>), StatusCodes.Status400BadRequest)] |
|
|
|
public async Task<ActionResult<OperationResult<RegisterUserResponse>>> Register([FromBody] RegisterUserCommand command) |
|
|
|
public async Task<OperationResult<RegisterUserResponse>> Register([FromBody] RegisterUserCommand command) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
@ -256,13 +252,12 @@ public class AuthController : ApiController |
|
|
|
result.ErrorMessages?.FirstOrDefault()); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "用户 {UserName} 注册时发生异常", command.UserName); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<RegisterUserResponse>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<RegisterUserResponse>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -289,7 +284,7 @@ public class AuthController : ApiController |
|
|
|
[HttpPost("refresh-token")] |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<AuthenticateUserResponse>), StatusCodes.Status400BadRequest)] |
|
|
|
public async Task<ActionResult<OperationResult<AuthenticateUserResponse>>> RefreshToken([FromBody] RefreshTokenCommand command) |
|
|
|
public async Task<OperationResult<AuthenticateUserResponse>> RefreshToken([FromBody] RefreshTokenCommand command) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
@ -305,13 +300,12 @@ public class AuthController : ApiController |
|
|
|
result.ErrorMessages?.FirstOrDefault()); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "刷新令牌时发生异常"); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<AuthenticateUserResponse>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<AuthenticateUserResponse>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -338,7 +332,7 @@ public class AuthController : ApiController |
|
|
|
[HttpPost("verification-codes")] |
|
|
|
[ProducesResponseType(typeof(OperationResult<SendVerificationCodeResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<SendVerificationCodeResponse>), StatusCodes.Status400BadRequest)] |
|
|
|
public async Task<ActionResult<OperationResult<SendVerificationCodeResponse>>> SendVerificationCode( |
|
|
|
public async Task<OperationResult<SendVerificationCodeResponse>> SendVerificationCode( |
|
|
|
[FromBody] SendVerificationCodeCommand command) |
|
|
|
{ |
|
|
|
try |
|
|
@ -357,13 +351,12 @@ public class AuthController : ApiController |
|
|
|
result.ErrorMessages?.FirstOrDefault()); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "向邮箱 {Email} 发送验证码时发生异常", command.Email); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<SendVerificationCodeResponse>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<SendVerificationCodeResponse>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -391,7 +384,7 @@ public class AuthController : ApiController |
|
|
|
[HttpPost("verification-codes/verify")] |
|
|
|
[ProducesResponseType(typeof(OperationResult<VerifyCodeResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<VerifyCodeResponse>), StatusCodes.Status400BadRequest)] |
|
|
|
public async Task<ActionResult<OperationResult<VerifyCodeResponse>>> VerifyCode( |
|
|
|
public async Task<OperationResult<VerifyCodeResponse>> VerifyCode( |
|
|
|
[FromBody] VerifyCodeCommand command) |
|
|
|
{ |
|
|
|
try |
|
|
@ -410,13 +403,12 @@ public class AuthController : ApiController |
|
|
|
result.ErrorMessages?.FirstOrDefault()); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "验证邮箱 {Email} 的验证码时发生异常", command.Email); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<VerifyCodeResponse>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<VerifyCodeResponse>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -432,7 +424,7 @@ public class AuthController : ApiController |
|
|
|
[HttpGet("captcha")] |
|
|
|
[ProducesResponseType(typeof(OperationResult<GenerateCaptchaResponse>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<GenerateCaptchaResponse>), StatusCodes.Status500InternalServerError)] |
|
|
|
public async Task<ActionResult<OperationResult<GenerateCaptchaResponse>>> GetCaptcha() |
|
|
|
public async Task<OperationResult<GenerateCaptchaResponse>> GetCaptcha() |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
@ -455,13 +447,12 @@ public class AuthController : ApiController |
|
|
|
result.ErrorMessages?.FirstOrDefault()); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "生成图形验证码时发生异常"); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<GenerateCaptchaResponse>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<GenerateCaptchaResponse>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -488,7 +479,7 @@ public class AuthController : ApiController |
|
|
|
[HttpPost("logout")] |
|
|
|
[ProducesResponseType(typeof(OperationResult<bool>), StatusCodes.Status200OK)] |
|
|
|
[ProducesResponseType(typeof(OperationResult<bool>), StatusCodes.Status400BadRequest)] |
|
|
|
public async Task<ActionResult<OperationResult<bool>>> Logout([FromBody] LogoutCommand command) |
|
|
|
public async Task<OperationResult<bool>> Logout([FromBody] LogoutCommand command) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
@ -504,13 +495,12 @@ public class AuthController : ApiController |
|
|
|
result.ErrorMessages?.FirstOrDefault()); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
_logger.LogError(ex, "用户登出时发生异常"); |
|
|
|
return StatusCode(StatusCodes.Status500InternalServerError, |
|
|
|
OperationResult<bool>.CreateFailure("系统错误,请稍后重试")); |
|
|
|
return OperationResult<bool>.CreateFailure("系统错误,请稍后重试"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |