using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Volo.Abp.Account.Localization;
using Volo.Abp.Account.Web.Pages.Account;
namespace LY.MicroService.AuthServer.Pages.Account
{
public class VerifyCodeModel : AccountPageModel
{
[BindProperty]
public VerifyCodeInputModel Input { get; set; }
///
/// 双因素认证提供程序
///
[HiddenInput]
[BindProperty(SupportsGet = true)]
public string Provider { get; set; }
///
/// 重定向Url
///
[HiddenInput]
[BindProperty(SupportsGet = true)]
public string ReturnUrl { get; set; }
///
///
///
[HiddenInput]
[BindProperty(SupportsGet = true)]
public string ReturnUrlHash { get; set; }
///
/// 是否记住登录状态
///
[HiddenInput]
[BindProperty(SupportsGet = true)]
public bool RememberMe { get; set; }
public VerifyCodeModel()
{
LocalizationResourceType = typeof(AccountResource);
}
public virtual IActionResult OnGet()
{
Input = new VerifyCodeInputModel();
return Page();
}
public virtual async Task OnPostAsync()
{
// 验证用户登录状态
var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();
if (user == null)
{
Alerts.Warning(L["TwoFactorAuthenticationInvaidUser"]);
return Page();
}
// 双因素登录
var result = await SignInManager.TwoFactorSignInAsync(Provider, Input.VerifyCode, RememberMe, Input.RememberBrowser);
if (result.Succeeded)
{
return await RedirectSafelyAsync(ReturnUrl, ReturnUrlHash);
}
if (result.IsLockedOut)
{
Logger.LogWarning(7, "User account locked out.");
Alerts.Warning(L["UserLockedOutMessage"]);
return Page();
}
else
{
Alerts.Danger(L["TwoFactorAuthenticationInvaidUser"]);// TODO: 更多状态码的解读
return Page();
}
}
}
public class VerifyCodeInputModel
{
///
/// 是否在浏览器中记住登录状态
///
public bool RememberBrowser { get; set; }
///
/// 发送的验证码
///
[Required]
public string VerifyCode { get; set; }
}
}