Skip to content

Commit 3a22fca

Browse files
committed
密码登录开启2FA
根据环境变量STS_PASSWORD_LOGIN_TWOFACTOR 判断是否开启密码2FA登录
1 parent 42db73c commit 3a22fca

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/SecurityTokenService/Controllers/AccountController.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class AccountController(
4545
private readonly SecurityTokenServiceOptions _options = options.CurrentValue;
4646
private readonly IdentityExtensionOptions _identityExtensionOptions = identityExtensionOptions.CurrentValue;
4747

48+
private static readonly bool PasswordLoginTwoFactorEnable =
49+
bool.Parse(Environment.GetEnvironmentVariable("STS_PASSWORD_LOGIN_TWOFACTOR") ?? "false");
50+
4851
/// <summary>
4952
/// 通过旧密码修改密码
5053
/// 要提供用户名
@@ -214,6 +217,19 @@ await events.RaiseAsync(new UserLoginFailureEvent(model.Username, "invalid crede
214217
{
215218
await events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id, user.UserName,
216219
clientId: context?.Client.ClientId));
220+
if (PasswordLoginTwoFactorEnable)
221+
{
222+
var isValid = await userManager.VerifyUserTokenAsync(user, Util.PhoneNumberTokenProvider,
223+
Util.PurposeLogin,
224+
model.VerifyCode);
225+
if (!isValid)
226+
{
227+
return new ObjectResult(new ApiResult
228+
{
229+
Code = Errors.VerifyCodeIsInCorrect, Success = false, Message = "手机验证码不正确"
230+
});
231+
}
232+
}
217233

218234
if (context != null)
219235
{

src/SecurityTokenService/Controllers/Inputs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ public class LoginInput
199199
/// </summary>
200200
[StringLength(10, ErrorMessage = "验证码长度超长"), Required(ErrorMessage = "请输入验证码")]
201201
public string CaptchaCode { get; set; }
202+
203+
/// <summary>
204+
/// 验证码
205+
/// </summary>
206+
[StringLength(8, ErrorMessage = "验证码长度不正确")]
207+
public string VerifyCode { get; set; }
202208
}
203209

204210
public class LogoutInput

0 commit comments

Comments
 (0)