Integrating DUO WEB with .NET core

Viewed 842

I am using Google Authenticator for 2FA on my .NET core application, framework 2.2.

Its working perfectly fine. Now, i want to replace that with Duo. I integrated DUO in the app but in the identity framework integration with DUO is not working. Need help as i am not sure how to make it work.

So, two factor is enabled on my login account, it first logs me in when i provide credentials, then it asks for 2FA code (as it previously was with Google Authenticator), there i have replaced that code page with DUO page and it asks me now to send the PUSH, i press it and it sends me the push, and that bit is working fine.

Here's the code to send the push, this bit is working fine.

         if (result.RequiresTwoFactor)
         {
          var sign = Duo.Web.SignRequest(ikey, skey, akey, username);
          ViewBag.sig_request = sign;
          return View("DuoLogin");
         }

Tried the following for two factor authentication, giving me failure in result.

 string authenticated_username = Duo.Web.VerifyResponse(ikey, skey, akey, sig_response);

  if (authenticated_username == username)
  {
   ViewBag.Message = "authenticated Successfully";
  
 var result = await _signInManager.TwoFactorAuthenticatorSignInAsync(authenticated_username., rememberMe, Input.RememberMachine);

 //OR 
var result = _signInManager.TwoFactorAuthenticatorSignInAsync(authenticated_username, true, false);

//OR
var result = _signInManager.TwoFactorSignInAsync("Duo", sig_response, true, false);
}

The authenticate_username only brings back the username of DUO, there's no code to pass to 'TwoFactorSignInAsync' method.

UPDATE:

So, if you scan the QR code with DUO, you can use DUO in place of Google authenticator and it works fine. But still i would prefer to have a push authentication with DUO enabled.

1 Answers

I solved this by creating a new LoginWithDuo razor page. The view holds the Duo html and javascript right off their site. The post_action is set to 'LoginWithDuo'. In the code below, _duoService simply handles the Duo.Web.xxxxx method calls and gets keys from settings. In Login.cshtml.cs, I replaced the ./LoginWIth2fa with ./LoginWithDuo in the result.RequiresTwoFactor block in the Post action.

public async Task<IActionResult> OnPostAsync(string sig_response, bool rememberMe, string returnUrl = null)
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        returnUrl = returnUrl ?? Url.Content("~/");

        var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
        if (user == null)
        {
            throw new InvalidOperationException($"Unable to load two-factor authentication user.");
        }

        string authenticatedUsername = _duoService.Verify(sig_response);

        if(authenticatedUsername == "username of logging in user")
        {
            // Cleanup two factor user id cookie 
            await _signInManager.SignOutAsync();

            await _signInManager.SignInAsync(user, isPersistent, "Duo");
            return LocalRedirect(returnUrl);
        }
        else
        {
            _logger.LogWarning("Invalid authenticator code entered for user with ID '{UserId}'.", user.Id);
            ModelState.AddModelError(string.Empty, "Invalid Duo match.");
            return Page();
        }

    }
Related