You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> This article shows the use of connection strings. When using a local database for development and testing, database user authentication via the connection string isn't required. In production environments, connection strings sometimes include a password to authenticate database access or database operations. A resource owner password credential (ROPC) in a connection string is a security risk to avoid in production apps. Production apps should use the most secure authentication flow available. For more information on authentication for apps deployed to test or production environments, see <xref:security/index#secure-authentication-flows>.
8
+
> This article shows the use of connection strings. When a local database is used for development and testing, database user authentication via the connection string isn't required.
9
+
> In production environments, connection strings sometimes include a password for authenticating database access or database operations. A resource owner password credential (ROPC) in a connection string is a security risk that should be avoided in production apps. Production apps should use the most secure authentication flow available.
10
+
> For more information on authentication for apps deployed to test or production environments, see <xref:security/index#secure-authentication-flows>.
title: Enable QR code generation for TOTP authenticator apps in ASP.NET Core
2
+
title: Enable QR code generation for TOTP authentication
3
3
ai-usage: ai-assisted
4
4
author: wadepickett
5
-
description: Discover how to enable QR code generation for TOTP authenticator apps that work with ASP.NET Core two-factor authentication.
5
+
description: Discover how to enable QR code generation for time-based one-time password (TOTP) authenticator apps that work with ASP.NET Core two-factor authentication.
# customer intent: As an ASP.NET developer, I want to enable QR code generation for TOTP authenticator apps, so I can support two-factor authentication.
11
13
---
12
14
13
15
# Enable QR code generation for TOTP authenticator apps in ASP.NET Core
@@ -19,7 +21,7 @@ ASP.NET Core includes support for authenticator applications for individual auth
19
21
- An authenticator app provides a 6 to 8 digit code that users enter after confirming their username and password.
20
22
- Typically, users install an authenticator app on a smartphone.
21
23
22
-
> [!WARNING]
24
+
> [!IMPORTANT]
23
25
> Keep an ASP.NET Core TOTP code secret because it can be used to authenticate successfully multiple times before it expires.
24
26
25
27
:::moniker range=">= aspnetcore-8.0"
@@ -37,56 +39,59 @@ The ASP.NET Core web app templates support authenticators but don't provide supp
37
39
38
40
:::moniker-end
39
41
40
-
Two-factor authentication doesn't happen by using an external authentication provider, such as [Google](xref:security/authentication/google-logins) or [Facebook](xref:security/authentication/facebook-logins). External logins are protected by whatever mechanism the external login provider provides. For example, the [Microsoft](xref:security/authentication/microsoft-logins) authentication provider requires a hardware key or another 2FA approach. If the default templates required 2FA for both the web app and the external authentication provider, users would need to satisfy two 2FA approaches. Requiring two 2FA approaches deviates from established security practices, which typically rely on a single, strong 2FA method for authentication.
42
+
Two-factor authentication doesn't happen by using an external authentication provider, such as [Google](xref:security/authentication/google-logins) or [Facebook](xref:security/authentication/facebook-logins). External sign ins are protected by whatever mechanism the external sign-in provider supports. For example, the [Microsoft](xref:security/authentication/microsoft-logins) authentication provider requires a hardware key or another 2FA approach. If the default templates required 2FA for both the web app and the external authentication provider, users need to satisfy two 2FA approaches. Requiring two 2FA approaches deviates from established security practices, which typically rely on a single, strong 2FA method for authentication.
41
43
42
-
## Adding QR codes to the 2FA configuration page
44
+
## Add QR codes to the 2FA configuration page
43
45
44
-
These instructions use `qrcode.js` from the [https://davidshimjs.github.io/qrcodejs/](https://davidshimjs.github.io/qrcodejs/) repo.
46
+
The following instructions use the _qrcode.js_ file from the [https://davidshimjs.github.io/qrcodejs/](https://davidshimjs.github.io/qrcodejs/) repo.
45
47
46
-
* Download the [`qrcode.js` JavaScript library](https://davidshimjs.github.io/qrcodejs/) to the `wwwroot\lib` folder in your project.
47
-
* Follow the instructions in [Scaffold Identity](xref:security/authentication/scaffold-identity) to generate `/Areas/Identity/Pages/Account/Manage/EnableAuthenticator.cshtml`.
48
-
* In `/Areas/Identity/Pages/Account/Manage/EnableAuthenticator.cshtml`, locate the `Scripts` section at the end of the file:
48
+
1. Download the ['qrcode.js' JavaScript library](https://davidshimjs.github.io/qrcodejs/) to the _wwwroot\lib_ folder in your project.
49
49
50
-
```cshtml
51
-
@section Scripts {
52
-
<partial name="_ValidationScriptsPartial" />
53
-
}
54
-
```
55
-
* Create a new JavaScript file called `qr.js` in `wwwroot/js` and add the following code to generate the QR code:
1. Follow the instructions in [Scaffold Identity](xref:security/authentication/scaffold-identity) to generate the _/Areas/Identity/Pages/Account/Manage/EnableAuthenticator.cshtml_ file.
68
51
69
-
* Update the `Scripts` section to add a reference to the `qrcode.js` library you previously downloaded.
70
-
* Add the `qr.js` file with the call to generate the QR code:
52
+
1. In the _/Areas/Identity/Pages/Account/Manage/EnableAuthenticator.cshtml_ file, locate the `Scripts` section at the end of the file:
1. Delete the paragraph that links you to these instructions.
87
+
88
+
1. Run your app. Confirm you can scan the QR code and validate the code the authenticator provides.
84
89
85
90
## Change the site name in the QR code
86
91
87
-
The site name in the QR code comes from the project name you choose when initially creating your project. You can change it by looking for the `GenerateQrCodeUri(string email, string unformattedKey)` method in the `/Areas/Identity/Pages/Account/Manage/EnableAuthenticator.cshtml.cs`.
92
+
The site name in the QR code comes from the project name you select when you create your project. You can change it by looking for the `GenerateQrCodeUri(string email, string unformattedKey)` method in the _/Areas/Identity/Pages/Account/Manage/EnableAuthenticator.cshtml.cs_ file.
88
93
89
-
The default code from the template looks as follows:
The second parameter in the call to `string.Format` is your site name, taken from your solution name. You can change it to any value, but it must always be URL encoded.
107
+
The second parameter in the call to `string.Format` is your site name, which is obtained from your solution name. You can change it to any value, but it must always be URL encoded.
103
108
104
-
## Using a different QR code library
109
+
## Use a different QR code library
105
110
106
111
You can replace the QR code library with your preferred library. The HTML contains a `qrCode` element into which you can place a QR code by whatever mechanism your library provides.
107
112
108
-
You can find the correctly formatted URL for the QR code in the:
113
+
You can find the correctly formatted URL for the QR code in the following locations:
114
+
115
+
*`AuthenticatorUri` property of the model
116
+
*`data-url` property in the `qrCodeData` element
117
+
118
+
## Check TOTP client and server times
109
119
110
-
*`AuthenticatorUri` property of the model.
111
-
*`data-url` property in the `qrCodeData` element.
120
+
TOTP (Time-based One-Time Password) authentication depends on both the server and authenticator device having an accurate time. Tokens only last for 30 seconds. If TOTP 2FA sign-in fails, confirm the server time is accurate, and preferably synchronized to an accurate NTP service.
112
121
113
-
## TOTP client and server time skew
122
+
## Related content
114
123
115
-
TOTP (Time-based One-Time Password) authentication depends on both the server and authenticator device having an accurate time. Tokens only last for 30 seconds. If TOTP 2FA logins fail, check that the server time is accurate, and preferably synchronized to an accurate NTP service.
This article shows how to call the [KeyDerivation.Pbkdf2](/dotnet/api/microsoft.aspnetcore.cryptography.keyderivation.keyderivation.pbkdf2) method, which allows hashing a password with the PBKDF2 algorithm, as described in [RFC 2898, Section 5.2](https://datatracker.ietf.org/doc/html/rfc2898#section-5.2).
15
15
16
+
The `KeyDerivation.Pbkdf2` API is a low-level cryptographic primitive. The intended use is for integrating apps into an existing protocol or cryptographic system.
17
+
16
18
> [!WARNING]
17
-
> The `KeyDerivation.Pbkdf2` API is a low-level cryptographic primitive. The intended use is for integrating apps into an existing protocol or cryptographic system. `KeyDerivation.Pbkdf2` shouldn't be used in new apps that support password-based sign in and which need to store hashed passwords in a datastore. New apps should use the [PasswordHasher](/dotnet/api/microsoft.aspnetcore.identity.passwordhasher-1) class. For more information, see [Exploring the ASP.NET Core Identity PasswordHasher](https://andrewlock.net/exploring-the-asp-net-core-identity-passwordhasher/).
19
+
> `KeyDerivation.Pbkdf2` shouldn't be used in new apps that support password-based sign in and which need to store hashed passwords in a datastore. New apps should use the [PasswordHasher](/dotnet/api/microsoft.aspnetcore.identity.passwordhasher-1) class. For more information, see [Exploring the ASP.NET Core Identity PasswordHasher](https://andrewlock.net/exploring-the-asp-net-core-identity-passwordhasher/).
18
20
19
21
The data protection code base includes a NuGet package [Microsoft.AspNetCore.Cryptography.KeyDerivation](https://www.nuget.org/packages/Microsoft.AspNetCore.Cryptography.KeyDerivation/) that contains cryptographic key derivation functions. This package is a standalone component and has no dependencies on the rest of the data protection system. The package can be used independently. The source exists alongside the data protection code base as a convenience.
20
22
23
+
## Generate key with 'KeyDerivation.Pbkdf2'
24
+
25
+
The following code shows how to use the `KeyDerivation.Pbkdf2` method to generate a shared secret key.
26
+
21
27
> [!WARNING]
22
-
> The following code shows how to use `KeyDerivation.Pbkdf2`to generate a shared secret key. It shouldn't be used to hash a password for storage in a datastore.
28
+
> Don't call the `KeyDerivation.Pbkdf2`method to hash a password for storage in a datastore.
23
29
24
30
<!-- See https://github.com/dotnet/AspNetCore.Docs/pull/26253#issuecomment-1187984822 for detailed reasoning -->
0 commit comments