我正在WebAPI,ASP.NET身份和OWIN中实现2个因子身份验证。每次我登录时,我都会获得signInstatus =成功从启用用户twofactorauthentication,从未达到signInstatus = sircessverification。
以下是一些代码片段,startup.cs:
private void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
app.UseOAuthBearerTokens(OAuthOptions);
}
Action method for enabling two factor authentication,
[HttpPost]
public async Task<IHttpActionResult> EnableTwoFactorAuthentication()
{
var user = await this.AppUserManager.FindByIdAsync(User.Identity.GetUserId());
if (user != null)
{
IdentityResult result = await this.AppUserManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId(), true);
await this.AppSignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
}
return Ok();
}
请建议解决方案。
如果您陷入这里,解决问题的一种方法是将方法从signinmanager直接复制到您的代码中并致电这些方法,以便您可以逐步介绍该方法并查看为什么您是获得错误的状态。对我来说,问题最终是我用:
实例化UserManager
new MyUserManager()
而不是正确的方式:
HttpContext.GetOwinContext().Get<MyUserManager>()
我将其用作设置的模板:https://github.com/adamtuliper/asp.net-indentity-samples/tree/master/basictemplate - - two factor/basictemplate
signManager return需要verseverification,如果:
dbo.aspnetusers为用户设置为true Twofactorenabled和Email Conderculd和用户电子邮件,请确认,电子邮件不要为空或null。
var result = SignInManager.PasswordSignIn(usernameIdentity, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", returnUrl);
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid username or password.");
return View(model);
}