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
* ROPC: connections strs and includes
* ROPC: connections strs and includes
* ROPC: connections strs and includes
* ROPC: connections strs and includes
* ROPC: connections strs and includes
* ROPC: connections strs and includes
* ROPC: connections strs and includes
description: A local database that doesn't require the user to be authenticated
3
+
author: rick-anderson
4
+
ms.author: riande
5
+
ms.date: 10/23/2024
6
+
ms.topic: include
7
+
---
8
+
> [!WARNING]
9
+
> This article uses a local database that doesn't require the user to be authenticated. Production apps should use the most secure authentication flow available. For more information on authentication for deployed test and production apps, see [Secure authentication flows](xref:security/index#secure-authentication-flows).
Most database providers require some form of connection string to connect to the database. Sometimes this connection string contains sensitive information that needs to be protected. You may also need to change the connection string as you move your application between environments, such as development, testing, and production.
10
+
Most database providers require a connection string to connect to the database. The connection string:
11
+
12
+
* Can contain sensitive information that needs to be protected.
13
+
* May need to change when the app moves to different environments, such as development, testing, and production.
14
+
15
+
For more information, see [Secure authentication flows](/aspnet/core/security/#secure-authentication-flows)
11
16
12
17
## ASP.NET Core
13
18
14
-
In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in `appsettings.json`, an environment variable, the user secret store, or another configuration source. See the [Configuration section of the ASP.NET Core documentation](/aspnet/core/fundamentals/configuration) for more details.
19
+
The ASP.NET Core configuration can store connection strings with various providers:
20
+
21
+
* In the `appsettings.Development.json` or `appsettings.json` file.
22
+
* In an environment variable
23
+
* Using the [Secret Manager tool](/aspnet/core/security/app-secrets#secret-manager)
24
+
25
+
> [!WARNING]
26
+
> Secrets should never be added to configuration files.
15
27
16
-
For instance, you can use the [Secret Manager tool](/aspnet/core/security/app-secrets#secret-manager) to store your database password and then, in scaffolding, use a connection string that simply consists of `Name=<database-alias>`.
28
+
For example, the [Secret Manager tool](/aspnet/core/security/app-secrets#secret-manager) can store the database password. When scaffolding and using Secret manager, a connection string consists of `Name=<database-alias>`.
29
+
30
+
See the [Configuration section of the ASP.NET Core documentation](/aspnet/core/fundamentals/configuration) for more information.
17
31
18
32
```dotnetcli
19
33
dotnet user-secrets set ConnectionStrings:YourDatabaseAlias "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=YourDatabase"
20
34
dotnet ef dbcontext scaffold Name=ConnectionStrings:YourDatabaseAlias Microsoft.EntityFrameworkCore.SqlServer
21
35
```
22
36
23
-
Or the following example shows the connection string stored in `appsettings.json`.
The following example shows the connection string stored in `appsettings.json`.
24
40
25
41
```json
26
42
{
@@ -30,19 +46,19 @@ Or the following example shows the connection string stored in `appsettings.json
30
46
}
31
47
```
32
48
33
-
Then the context is typically configured in `Startup.cs` with the connection string being read from configuration. Note the `GetConnectionString()` method looks for a configuration value whose key is `ConnectionStrings:<connection string name>`. You need to import the [Microsoft.Extensions.Configuration](/dotnet/api/microsoft.extensions.configuration) namespace to use this extension method.
49
+
The context is typically configured in `Program.cs` with the connection string being read from configuration. Note the [GetConnectionString](/dotnet/api/microsoft.extensions.configuration.configurationextensions.getconnectionstring) method looks for a configuration value whose key is `ConnectionStrings:<connection string name>`. `GetConnectionString` requires the [Microsoft.Extensions.Configuration](/dotnet/api/microsoft.extensions.configuration) namespace.
WinForms, WPF, and ASP.NET 4 applications have a tried and tested connection string pattern. The connection string should be added to your application's App.config file (Web.config if you are using ASP.NET). If your connection string contains sensitive information, such as username and password, you can protect the contents of the configuration file using [Protected Configuration](/dotnet/framework/data/adonet/connection-strings-and-configuration-files#encrypting-configuration-file-sections-using-protected-configuration).
61
+
WinForms, WPF, and ASP.NET 4 applications have a tried and tested connection string pattern. The connection string should be added to your application's `App.config` file, or `Web.config` when using ASP.NET. Connection string containing sensitive information, such as username and password, should protect the contents of the configuration file using [Protected Configuration](/dotnet/framework/data/adonet/connection-strings-and-configuration-files#encrypting-configuration-file-sections-using-protected-configuration).
46
62
47
63
```xml
48
64
<?xml version="1.0" encoding="utf-8"?>
@@ -75,7 +91,7 @@ public class BloggingContext : DbContext
75
91
76
92
## Universal Windows Platform (UWP)
77
93
78
-
Connection strings in a UWP application are typically a SQLite connection that just specifies a local filename. They typically do not contain sensitive information, and do not need to be changed as an application is deployed. As such, these connection strings are usually fine to be left in code, as shown below. If you wish to move them out of code then UWP supports the concept of settings, see the [App Settings section of the UWP documentation](/windows/uwp/app-settings/store-and-retrieve-app-data) for details.
94
+
Connection strings in a UWP application are typically a SQLite connection that just specifies a local filename. They typically don't contain sensitive information, and don't need to be changed as an application is deployed. As such, these connection strings are usually fine to be left in code, as shown below. If you wish to move them out of code then UWP supports the concept of settings, see the [App Settings section of the UWP documentation](/windows/uwp/app-settings/store-and-retrieve-app-data) for details.
0 commit comments