Skip to content

Commit eb105c0

Browse files
ROPC: connections strs and includes (#4842)
* 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
1 parent afc17d2 commit eb105c0

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
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).

entity-framework/core/miscellaneous/connection-strings.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,36 @@ uid: core/miscellaneous/connection-strings
77
---
88
# Connection Strings
99

10-
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)
1116

1217
## ASP.NET Core
1318

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.
1527
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.
1731

1832
```dotnetcli
1933
dotnet user-secrets set ConnectionStrings:YourDatabaseAlias "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=YourDatabase"
2034
dotnet ef dbcontext scaffold Name=ConnectionStrings:YourDatabaseAlias Microsoft.EntityFrameworkCore.SqlServer
2135
```
2236

23-
Or the following example shows the connection string stored in `appsettings.json`.
37+
[!INCLUDE [managed-identities-test-non-production](~/core/includes/managed-identities-test-non-production.md)]
38+
39+
The following example shows the connection string stored in `appsettings.json`.
2440

2541
```json
2642
{
@@ -30,19 +46,19 @@ Or the following example shows the connection string stored in `appsettings.json
3046
}
3147
```
3248

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.
3450

3551
```csharp
36-
public void ConfigureServices(IServiceCollection services)
37-
{
38-
services.AddDbContext<BloggingContext>(options =>
39-
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
40-
}
52+
var conString = builder.Configuration.GetConnectionString("BloggingContext") ??
53+
throw new InvalidOperationException("Connection string 'BloggingContext'" +
54+
" not found.");
55+
builder.Services.AddDbContext<BloggingContext>(options =>
56+
options.UseSqlServer(conString));
4157
```
4258

4359
## WinForms & WPF Applications
4460

45-
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).
4662

4763
```xml
4864
<?xml version="1.0" encoding="utf-8"?>
@@ -75,7 +91,7 @@ public class BloggingContext : DbContext
7591

7692
## Universal Windows Platform (UWP)
7793

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.
7995

8096
```csharp
8197
public class BloggingContext : DbContext

0 commit comments

Comments
 (0)