Skip to content

Commit ed7cb0c

Browse files
authored
Merge pull request #13 from fzxaasd/main
密码登录开启2FA
2 parents 42db73c + d3eecb1 commit ed7cb0c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/SecurityTokenService/Controllers/AccountController.cs

Lines changed: 24 additions & 3 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
/// 要提供用户名
@@ -190,10 +193,13 @@ public async Task<IActionResult> Login([FromBody] Inputs.V1.LoginInput model)
190193
return new ObjectResult(new RedirectResult("/"));
191194
}
192195

193-
var checkCaptchaResult = Util.CheckCaptcha(memoryCache, logger, Request, model.CaptchaCode);
194-
if (checkCaptchaResult != null)
196+
if (!PasswordLoginTwoFactorEnable)
195197
{
196-
return new ObjectResult(checkCaptchaResult);
198+
var checkCaptchaResult = Util.CheckCaptcha(memoryCache, logger, Request, model.CaptchaCode);
199+
if (checkCaptchaResult != null)
200+
{
201+
return new ObjectResult(checkCaptchaResult);
202+
}
197203
}
198204

199205
var user = await userManager.FindAsync(model.Username, _identityExtensionOptions.SoftDeleteColumn);
@@ -208,13 +214,28 @@ await events.RaiseAsync(new UserLoginFailureEvent(model.Username, "invalid crede
208214
});
209215
}
210216

217+
if (PasswordLoginTwoFactorEnable)
218+
{
219+
var isValid = await userManager.VerifyUserTokenAsync(user, Util.PhoneNumberTokenProvider,
220+
Util.PurposeLogin,
221+
model.VerifyCode);
222+
if (!isValid)
223+
{
224+
return new ObjectResult(new ApiResult
225+
{
226+
Code = Errors.VerifyCodeIsInCorrect, Success = false, Message = "手机验证码不正确"
227+
});
228+
}
229+
}
230+
211231
var result = await signInManager.PasswordSignInAsync(user, model.Password,
212232
model.RememberLogin, true);
213233
if (result.Succeeded)
214234
{
215235
await events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id, user.UserName,
216236
clientId: context?.Client.ClientId));
217237

238+
218239
if (context != null)
219240
{
220241
// if (await _clientStore.IsPkceClientAsync(context.Client.ClientId))

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)