# How to verify if TOTP code is correct in code?

**URL:** <https://community.auth0.com/t/how-to-verify-if-totp-code-is-correct-in-code/122232>\
**Category:** Get Help\
**Tags:** one-time-password-otp-factor, mfa\
**Created:** [December 3, 2023, 7:38pm UTC](https://community.auth0.com/t/how-to-verify-if-totp-code-is-correct-in-code/122232 "2023-12-03T19:38:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![romko](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@romko](https://community.auth0.com/u/romko)\
**Post date:** [December 3, 2023, 7:38pm UTC](https://community.auth0.com/t/how-to-verify-if-totp-code-is-correct-in-code/122232/1 "2023-12-03T19:38:55Z")

</div>

Hi, we have a case where we need to ask user for additional verification to perform some sensitive tasks on our api. Users have set-up 2fa totp, and they would send this code on api call. How do we actually verify if this code is valid at the time for the given user? I haven’t found anything like this in mgmt api. Is there a specific api for this? This is crucial for us. And example in curl would suffice, but would be ideal with dotnet.

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [December 4, 2023, 9:23am UTC](https://community.auth0.com/t/how-to-verify-if-totp-code-is-correct-in-code/122232/2 "2023-12-04T09:23:02Z")

</div>

Hi @romko , welcome to Auth0!

> [@romko](#):
>
> How do we actually verify if this code is valid at the time for the given user?

If your users are enrolled to MFA, you can set your app to prompt them to authenticate again when they want to enter your API. So basically, even if they have already been logged in, repeat the authentication for them if they are invoking your API. This way you are sure that only users that are double checked at a given moment in time are entering your API.

> [@romko](#):
>
> And example in curl would suffice, but would be ideal with dotnet.

The final verification of the OTP happens against the Auth0 Authentication API so you do not need to validate it yourself.

I do not know the .NET snipped for that, but found a sample CURL of the final OPT verification resulting in receiving new access and id tokens in case of a [Resource Owner Passwords flow](https://auth0.com/docs/secure/multi-factor-authentication/authenticate-using-ropg-flow-with-mfa/enroll-and-challenge-otp-authenticators#complete-authentication-using-received-code).

```auto
curl --request POST \
  --url 'https://{yourDomain}/oauth/token' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data grant_type=http://auth0.com/oauth/grant-type/mfa-otp \
  --data 'client_id={yourClientId}' \
  --data 'client_secret={yourClientSecret}' \
  --data 'mfa_token={mfaToken}' \
  --data 'otp={userOtpCode}'

```

Hope this helps and please let me know any questions!

---

<div class="post-metadata">

**Author:** ![romko](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@romko](https://community.auth0.com/u/romko)\
**Post date:** [December 4, 2023, 9:45am UTC](https://community.auth0.com/t/how-to-verify-if-totp-code-is-correct-in-code/122232/3 "2023-12-04T09:45:38Z")

</div>

Hi @marcelina.barycka, thanks for your reply.  
I do have more questions though:

1. Is this a step-up authentication (could be found in auth0 docs) flow that you’re suggesting, or something else?
2. And about this second authentication prompt, will the user have to enter his email and password again, + totp? Or I can redirect straight to totp?

p.s. we have asp net core web api as a back-end but out fron-end in reactjs spa.

---

<div class="post-metadata">

**Author:** ![marcelina.barycka](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marcelina.barycka/32/56162_2.png) [@marcelina.barycka](https://community.auth0.com/u/marcelina.barycka)\
**Post date:** [December 5, 2023, 9:07am UTC](https://community.auth0.com/t/how-to-verify-if-totp-code-is-correct-in-code/122232/5 "2023-12-05T09:07:51Z")

</div>

Hi @romko !

Thanks for you patience.

> [@romko](#):
>
> Is this a step-up authentication (could be found in auth0 docs) flow that you’re suggesting, or something else?

I referred to the [/authorize](https://auth0.com/docs/api/authentication#database-ad-ldap-passive-) request being sent when users log in or call the protected API endpoints.

> [@romko](#):
>
> And about this second authentication prompt, will the user have to enter his email and password again, + totp? Or I can redirect straight to totp?

If they are already logged in they would be challenged to provide the OPT.

> [@romko](#):
>
> p.s. we have asp net core web api as a back-end but out fron-end in reactjs spa.

For the single page application scenario, there is a step-up authentication doc that approach the problem with Actions that challenge with OPT when a SPA app requests the specific sensitive API scope using `event.transaction.requested_scopes.indexOf('sensitive_scope')`

> **[Configure Step-up Authentication for APIs](https://auth0.com/docs/secure/multi-factor-authentication/step-up-authentication/configure-step-up-authentication-for-apis#create-an-action)**
>
> Learn how an API can check if a user has logged in with Multi-factor Authentication by examining their access token.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 19, 2023, 9:08am UTC](https://community.auth0.com/t/how-to-verify-if-totp-code-is-correct-in-code/122232/6 "2023-12-19T09:08:01Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
