-
Notifications
You must be signed in to change notification settings - Fork 25.1k
ROPC remediation - more files with connection strings #33990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,13 @@ description: Learn how to store and retrieve sensitive information during the de | |
| ms.author: tdykstra | ||
| monikerRange: '>= aspnetcore-3.0' | ||
| ms.custom: mvc | ||
| ms.date: 10/29/2024 | ||
| ms.date: 10/30/2024 | ||
| uid: security/app-secrets | ||
| --- | ||
| <!-- ms.sfi.ropc: t --> | ||
| # Safe storage of app secrets in development in ASP.NET Core | ||
|
|
||
|
|
||
| [!INCLUDE[](~/includes/not-latest-version.md)] | ||
|
|
||
| :::moniker range=">= aspnetcore-6.0" | ||
|
|
@@ -19,7 +20,7 @@ By [Rick Anderson](https://twitter.com/RickAndMSFT) and [Kirk Larkin](https://tw | |
|
|
||
| [View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/security/app-secrets/samples) ([how to download](xref:index#how-to-download-a-sample)) | ||
|
|
||
| This article explains how to manage sensitive data for an ASP.NET Core app on a development machine. Never store passwords or other sensitive data in source code or configuration files. Production secrets shouldn't be used for development or test. Secrets shouldn't be deployed with the app. Production secrets should be accessed through a controlled means like Azure Key Vault. Azure test and production secrets can be stored and protected with the [Azure Key Vault configuration provider](xref:security/key-vault-configuration). | ||
| This article explains how to manage sensitive data for an ASP.NET Core app on a development machine. Never store passwords or other sensitive data in source code or configuration files. Production secrets shouldn't be used for development or test. Secrets shouldn't be deployed with the app. Production secrets should be accessed through a controlled means like Azure Key Vault. Azure test and production secrets can be stored and protected with the [Azure Key Vault configuration provider](xref:security/key-vault-configuration). | ||
|
|
||
| For more information on authentication for deployed test and production apps, see [Secure authentication flows](xref:security/index#secure-authentication-flows). | ||
|
|
||
|
|
@@ -201,21 +202,13 @@ The `Movies:ConnectionString` and `Movies:ServiceApiKey` secrets are mapped to t | |
|
|
||
| ## String replacement with secrets | ||
|
|
||
| Storing passwords in plain text is insecure. For example, a database connection string stored in `appsettings.json` may include a password for the specified user: | ||
|
|
||
| [!code-json[](~/security/app-secrets/samples/3.x/UserSecrets/appsettings-unsecure.json?highlight=3)] | ||
|
|
||
| A more secure approach is to store the password as a secret. For example: | ||
| Storing passwords in plain text is insecure. For example, a database connection string stored in `appsettings.json` should not include a password. Instead, store the password as a secret, and include the password in the connection string at runtime. For example: | ||
tdykstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```dotnetcli | ||
| dotnet user-secrets set "DbPassword" "pass123" | ||
| dotnet user-secrets set "DbPassword" "`<secret value>`" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For bonus points, replace " |
||
| ``` | ||
|
|
||
| Remove the `Password` key-value pair from the connection string in `appsettings.json`. For example: | ||
|
|
||
| [!code-json[](~/security/app-secrets/samples/3.x/UserSecrets/appsettings.json?highlight=3)] | ||
|
|
||
| The secret's value can be set on a <xref:System.Data.SqlClient.SqlConnectionStringBuilder> object's <xref:System.Data.SqlClient.SqlConnectionStringBuilder.Password%2A> property to complete the connection string: | ||
| Replace the `<secret value>` placeholder in the preceding example with the password value. Set the secret's value on a <xref:System.Data.SqlClient.SqlConnectionStringBuilder> object's <xref:System.Data.SqlClient.SqlConnectionStringBuilder.Password%2A> property to include it as the password value in the connection string: | ||
|
|
||
| [!code-csharp[](~/security/app-secrets/samples/6.x/UserSecrets/Program.cs?name=snippet_sql&highlight=5-8)] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ By [Rick Anderson](https://twitter.com/RickAndMSFT), [Kirk Larkin](https://twitt | |
| This article explains how to manage sensitive data for an ASP.NET Core app on a development machine. Never store passwords or other sensitive data in source code or configuration files. Production secrets shouldn't be used for development or test. Secrets shouldn't be deployed with the app. Production secrets should be accessed through a controlled means like Azure Key Vault. Azure test and production secrets can be stored and protected with the [Azure Key Vault configuration provider](xref:security/key-vault-configuration). | ||
|
|
||
| For more information on authentication for test and production environments, see [Secure authentication flows](xref:security/index#secure-authentication-flows). | ||
|
|
||
| ## Environment variables | ||
|
|
||
| Environment variables are used to avoid storage of app secrets in code or in local configuration files. Environment variables override configuration values for all previously specified configuration sources. | ||
|
|
@@ -172,23 +173,15 @@ The `Movies:ConnectionString` and `Movies:ServiceApiKey` secrets are mapped to t | |
|
|
||
| ## String replacement with secrets | ||
|
|
||
| Storing passwords in plain text is insecure. For example, a database connection string stored in `appsettings.json` may include a password for the specified user: | ||
|
|
||
| [!code-json[](~/security/app-secrets/samples/3.x/UserSecrets/appsettings-unsecure.json?highlight=3)] | ||
|
|
||
| A more secure approach is to store the password as a secret. For example: | ||
| Storing passwords in plain text is insecure. For example, a database connection string stored in `appsettings.json` should not include a password. Instead, store the password as a secret, and include the password in the connection string at runtime. For example: | ||
|
||
|
|
||
| ```dotnetcli | ||
| dotnet user-secrets set "DbPassword" "pass123" | ||
| dotnet user-secrets set "DbPassword" "<secret value>" | ||
| ``` | ||
|
|
||
| Remove the `Password` key-value pair from the connection string in `appsettings.json`. For example: | ||
|
|
||
| [!code-json[](~/security/app-secrets/samples/3.x/UserSecrets/appsettings.json?highlight=3)] | ||
|
|
||
| The secret's value can be set on a <xref:System.Data.SqlClient.SqlConnectionStringBuilder> object's <xref:System.Data.SqlClient.SqlConnectionStringBuilder.Password%2A> property to complete the connection string: | ||
| Replace the `<secret value>` placeholder in the preceding example with the password value. Set the secret's value on a <xref:System.Data.SqlClient.SqlConnectionStringBuilder> object's <xref:System.Data.SqlClient.SqlConnectionStringBuilder.Password%2A> property to include it as the password value in the connection string: | ||
|
|
||
| [!code-csharp[](~/security/app-secrets/samples/3.x/UserSecrets/Startup2.cs?name=snippet_StartupClass&highlight=14-17)] | ||
| [!code-csharp[](~/security/app-secrets/samples/6.x/UserSecrets/Program.cs?name=snippet_sql&highlight=5-8)] | ||
|
|
||
| ## List the secrets | ||
|
|
||
|
|
||
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Including password here isn't required, as it and the other parts of the connection string were there just to give an impression of what an Azure Redis Cache connection string looks like.