Using TotpSecurityStampBasedTokenProvider for TOTP with Google Authenticator












1















I'm implementing 2FA using TOTP on my asp.net web api 2 webservice (NOT .net core).



The implementation uses the TotpSecurityStampBasedTokenProvider which is provided by the Microsoft.AspNet.Identity framework.



When looking at Google Authenticator requirements it lists that the secret needs to be a base32 encoded string.



If you look at the code from the TotpSecurityStampBasedTokenProvider it uses the user's SecurityStamp as the secret which is generated by the Identity Framework. This is a GUID and not a base32 encoded string.



According to the specifications for the Google Authenticator there is a requirement that the secret is a base32 encoded string.



How can I use the TotpSecurityStampBasedTokenProvider for use with Google Authenticator?










share|improve this question





























    1















    I'm implementing 2FA using TOTP on my asp.net web api 2 webservice (NOT .net core).



    The implementation uses the TotpSecurityStampBasedTokenProvider which is provided by the Microsoft.AspNet.Identity framework.



    When looking at Google Authenticator requirements it lists that the secret needs to be a base32 encoded string.



    If you look at the code from the TotpSecurityStampBasedTokenProvider it uses the user's SecurityStamp as the secret which is generated by the Identity Framework. This is a GUID and not a base32 encoded string.



    According to the specifications for the Google Authenticator there is a requirement that the secret is a base32 encoded string.



    How can I use the TotpSecurityStampBasedTokenProvider for use with Google Authenticator?










    share|improve this question



























      1












      1








      1








      I'm implementing 2FA using TOTP on my asp.net web api 2 webservice (NOT .net core).



      The implementation uses the TotpSecurityStampBasedTokenProvider which is provided by the Microsoft.AspNet.Identity framework.



      When looking at Google Authenticator requirements it lists that the secret needs to be a base32 encoded string.



      If you look at the code from the TotpSecurityStampBasedTokenProvider it uses the user's SecurityStamp as the secret which is generated by the Identity Framework. This is a GUID and not a base32 encoded string.



      According to the specifications for the Google Authenticator there is a requirement that the secret is a base32 encoded string.



      How can I use the TotpSecurityStampBasedTokenProvider for use with Google Authenticator?










      share|improve this question
















      I'm implementing 2FA using TOTP on my asp.net web api 2 webservice (NOT .net core).



      The implementation uses the TotpSecurityStampBasedTokenProvider which is provided by the Microsoft.AspNet.Identity framework.



      When looking at Google Authenticator requirements it lists that the secret needs to be a base32 encoded string.



      If you look at the code from the TotpSecurityStampBasedTokenProvider it uses the user's SecurityStamp as the secret which is generated by the Identity Framework. This is a GUID and not a base32 encoded string.



      According to the specifications for the Google Authenticator there is a requirement that the secret is a base32 encoded string.



      How can I use the TotpSecurityStampBasedTokenProvider for use with Google Authenticator?







      c# asp.net-identity totp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 9:20







      Peter

















      asked Nov 28 '18 at 9:07









      PeterPeter

      9,1781461103




      9,1781461103
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The TotpSecurityStampBasedTokenProvider uses UserManager.CreateSecurityTokenAsync to generate the secret code which is used to calculate the token.



          The code relies on the user ID, and is generated into a byte array, which is usually shown as base-32 or QR code on-screen to the user in the TOTP setup phase.



          So long story short, there is nothing to worry about.






          share|improve this answer
























          • Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong.

            – Peter
            Nov 28 '18 at 9:42













          • That would be GetSecurityStampAsync or CreateSecurityTokenAsync probably.

            – Patrick Hofman
            Nov 28 '18 at 9:45






          • 1





            You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight!

            – Peter
            Nov 28 '18 at 9:50













          • Yes, indeed. Good you found it.

            – Patrick Hofman
            Nov 28 '18 at 9:51






          • 1





            The sample projects are usually quite useful. See for example here: github.com/aspnet/Identity/blob/master/samples/…

            – Patrick Hofman
            Nov 28 '18 at 9:51



















          0














          The problem is that the TotpSecurityStampBasedTokenProvider provided by Microsoft.AspNet.Identity.Core implements a hardcoded timestep of 3 minutes. Google Authenticator uses a default value of 30 seconds, which can NOT be modified according to the documentation provided.



          This causes different codes to be generated by both TOTP computations resulting in an always false authentication.



          I have added a ticket to the github repository of aspnet.identity about this.



          So for now nothing left for me to do than create my own totpProvider






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53515739%2fusing-totpsecuritystampbasedtokenprovider-for-totp-with-google-authenticator%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The TotpSecurityStampBasedTokenProvider uses UserManager.CreateSecurityTokenAsync to generate the secret code which is used to calculate the token.



            The code relies on the user ID, and is generated into a byte array, which is usually shown as base-32 or QR code on-screen to the user in the TOTP setup phase.



            So long story short, there is nothing to worry about.






            share|improve this answer
























            • Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong.

              – Peter
              Nov 28 '18 at 9:42













            • That would be GetSecurityStampAsync or CreateSecurityTokenAsync probably.

              – Patrick Hofman
              Nov 28 '18 at 9:45






            • 1





              You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight!

              – Peter
              Nov 28 '18 at 9:50













            • Yes, indeed. Good you found it.

              – Patrick Hofman
              Nov 28 '18 at 9:51






            • 1





              The sample projects are usually quite useful. See for example here: github.com/aspnet/Identity/blob/master/samples/…

              – Patrick Hofman
              Nov 28 '18 at 9:51
















            1














            The TotpSecurityStampBasedTokenProvider uses UserManager.CreateSecurityTokenAsync to generate the secret code which is used to calculate the token.



            The code relies on the user ID, and is generated into a byte array, which is usually shown as base-32 or QR code on-screen to the user in the TOTP setup phase.



            So long story short, there is nothing to worry about.






            share|improve this answer
























            • Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong.

              – Peter
              Nov 28 '18 at 9:42













            • That would be GetSecurityStampAsync or CreateSecurityTokenAsync probably.

              – Patrick Hofman
              Nov 28 '18 at 9:45






            • 1





              You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight!

              – Peter
              Nov 28 '18 at 9:50













            • Yes, indeed. Good you found it.

              – Patrick Hofman
              Nov 28 '18 at 9:51






            • 1





              The sample projects are usually quite useful. See for example here: github.com/aspnet/Identity/blob/master/samples/…

              – Patrick Hofman
              Nov 28 '18 at 9:51














            1












            1








            1







            The TotpSecurityStampBasedTokenProvider uses UserManager.CreateSecurityTokenAsync to generate the secret code which is used to calculate the token.



            The code relies on the user ID, and is generated into a byte array, which is usually shown as base-32 or QR code on-screen to the user in the TOTP setup phase.



            So long story short, there is nothing to worry about.






            share|improve this answer













            The TotpSecurityStampBasedTokenProvider uses UserManager.CreateSecurityTokenAsync to generate the secret code which is used to calculate the token.



            The code relies on the user ID, and is generated into a byte array, which is usually shown as base-32 or QR code on-screen to the user in the TOTP setup phase.



            So long story short, there is nothing to worry about.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 28 '18 at 9:26









            Patrick HofmanPatrick Hofman

            128k18177235




            128k18177235













            • Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong.

              – Peter
              Nov 28 '18 at 9:42













            • That would be GetSecurityStampAsync or CreateSecurityTokenAsync probably.

              – Patrick Hofman
              Nov 28 '18 at 9:45






            • 1





              You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight!

              – Peter
              Nov 28 '18 at 9:50













            • Yes, indeed. Good you found it.

              – Patrick Hofman
              Nov 28 '18 at 9:51






            • 1





              The sample projects are usually quite useful. See for example here: github.com/aspnet/Identity/blob/master/samples/…

              – Patrick Hofman
              Nov 28 '18 at 9:51



















            • Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong.

              – Peter
              Nov 28 '18 at 9:42













            • That would be GetSecurityStampAsync or CreateSecurityTokenAsync probably.

              – Patrick Hofman
              Nov 28 '18 at 9:45






            • 1





              You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight!

              – Peter
              Nov 28 '18 at 9:50













            • Yes, indeed. Good you found it.

              – Patrick Hofman
              Nov 28 '18 at 9:51






            • 1





              The sample projects are usually quite useful. See for example here: github.com/aspnet/Identity/blob/master/samples/…

              – Patrick Hofman
              Nov 28 '18 at 9:51

















            Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong.

            – Peter
            Nov 28 '18 at 9:42







            Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong.

            – Peter
            Nov 28 '18 at 9:42















            That would be GetSecurityStampAsync or CreateSecurityTokenAsync probably.

            – Patrick Hofman
            Nov 28 '18 at 9:45





            That would be GetSecurityStampAsync or CreateSecurityTokenAsync probably.

            – Patrick Hofman
            Nov 28 '18 at 9:45




            1




            1





            You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight!

            – Peter
            Nov 28 '18 at 9:50







            You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight!

            – Peter
            Nov 28 '18 at 9:50















            Yes, indeed. Good you found it.

            – Patrick Hofman
            Nov 28 '18 at 9:51





            Yes, indeed. Good you found it.

            – Patrick Hofman
            Nov 28 '18 at 9:51




            1




            1





            The sample projects are usually quite useful. See for example here: github.com/aspnet/Identity/blob/master/samples/…

            – Patrick Hofman
            Nov 28 '18 at 9:51





            The sample projects are usually quite useful. See for example here: github.com/aspnet/Identity/blob/master/samples/…

            – Patrick Hofman
            Nov 28 '18 at 9:51













            0














            The problem is that the TotpSecurityStampBasedTokenProvider provided by Microsoft.AspNet.Identity.Core implements a hardcoded timestep of 3 minutes. Google Authenticator uses a default value of 30 seconds, which can NOT be modified according to the documentation provided.



            This causes different codes to be generated by both TOTP computations resulting in an always false authentication.



            I have added a ticket to the github repository of aspnet.identity about this.



            So for now nothing left for me to do than create my own totpProvider






            share|improve this answer




























              0














              The problem is that the TotpSecurityStampBasedTokenProvider provided by Microsoft.AspNet.Identity.Core implements a hardcoded timestep of 3 minutes. Google Authenticator uses a default value of 30 seconds, which can NOT be modified according to the documentation provided.



              This causes different codes to be generated by both TOTP computations resulting in an always false authentication.



              I have added a ticket to the github repository of aspnet.identity about this.



              So for now nothing left for me to do than create my own totpProvider






              share|improve this answer


























                0












                0








                0







                The problem is that the TotpSecurityStampBasedTokenProvider provided by Microsoft.AspNet.Identity.Core implements a hardcoded timestep of 3 minutes. Google Authenticator uses a default value of 30 seconds, which can NOT be modified according to the documentation provided.



                This causes different codes to be generated by both TOTP computations resulting in an always false authentication.



                I have added a ticket to the github repository of aspnet.identity about this.



                So for now nothing left for me to do than create my own totpProvider






                share|improve this answer













                The problem is that the TotpSecurityStampBasedTokenProvider provided by Microsoft.AspNet.Identity.Core implements a hardcoded timestep of 3 minutes. Google Authenticator uses a default value of 30 seconds, which can NOT be modified according to the documentation provided.



                This causes different codes to be generated by both TOTP computations resulting in an always false authentication.



                I have added a ticket to the github repository of aspnet.identity about this.



                So for now nothing left for me to do than create my own totpProvider







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 '18 at 15:45









                PeterPeter

                9,1781461103




                9,1781461103






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53515739%2fusing-totpsecuritystampbasedtokenprovider-for-totp-with-google-authenticator%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown