SignInStatus always returns Success on TwoFactorAuthentication is enabled in webapi using asp.net identity

久未见 提交于 2019-12-11 09:49:10

问题


I am implementing 2 factor authentication in WebApi, asp.net identity and OWIN. Every time I log in, I get SignInStatus = Success never reaches to SignInStatus = RequiresVerification though user TwoFactorAuthentication is enabled.

Below are some code snippets, 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();
        }

Please suggest a solution.


回答1:


If you get stuck here, one way to solve the problem is to copy the methods from SignInManager directly into your code and call those instead so you can step through the methods and see why you are getting the wrong status. For me the problem ended up being that I instantiated my UserManager with:

new MyUserManager() 

instead of the right way:

HttpContext.GetOwinContext().Get<MyUserManager>()

I was using this as my template for setting it up: https://github.com/adamtuliper/ASP.NET-Identity-Samples/tree/master/BasicTemplate%20-%20Two%20Factor/BasicTemplate



来源:https://stackoverflow.com/questions/48744934/signinstatus-always-returns-success-on-twofactorauthentication-is-enabled-in-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!