Skip to content

Commit 7410ba1

Browse files
authored
Show AddUserSecrets without CreateDefaultBuilder (#17945)
1 parent 524c20b commit 7410ba1

12 files changed

Lines changed: 76 additions & 210 deletions

File tree

aspnetcore/security/app-secrets.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ dotnet user-secrets init
6969

7070
The preceding command adds a `UserSecretsId` element within a `PropertyGroup` of the *.csproj* file. By default, the inner text of `UserSecretsId` is a GUID. The inner text is arbitrary, but is unique to the project.
7171

72-
[!code-xml[](app-secrets/samples/2.x/UserSecrets/UserSecrets.csproj?name=snippet_PropertyGroup&highlight=3)]
72+
[!code-xml[](app-secrets/samples/3.x/UserSecrets/UserSecrets.csproj?name=snippet_PropertyGroup&highlight=3)]
7373

7474
In Visual Studio, right-click the project in Solution Explorer, and select **Manage User Secrets** from the context menu. This gesture adds a `UserSecretsId` element, populated with a GUID, to the *.csproj* file.
7575

@@ -136,18 +136,17 @@ Open a command shell, and execute the following command:
136136

137137
The [ASP.NET Core Configuration API](xref:fundamentals/configuration/index) provides access to Secret Manager secrets.
138138

139-
In ASP.NET Core 2.0 or later, the user secrets configuration source is automatically added in development mode when the project calls <xref:Microsoft.AspNetCore.WebHost.CreateDefaultBuilder%2A> to initialize a new instance of the host with preconfigured defaults. `CreateDefaultBuilder` calls <xref:Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets%2A> when the <xref:Microsoft.AspNetCore.Hosting.IHostingEnvironment.EnvironmentName> is <xref:Microsoft.AspNetCore.Hosting.EnvironmentName.Development>:
139+
The user secrets configuration source is automatically added in development mode when the project calls <xref:Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder%2A> to initialize a new instance of the host with preconfigured defaults. `CreateDefaultBuilder` calls <xref:Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets%2A> when the <xref:Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName> is <xref:Microsoft.Extensions.Hosting.EnvironmentName.Development>:
140140

141141
[!code-csharp[](app-secrets/samples/3.x/UserSecrets/Program.cs?name=snippet_CreateHostBuilder&highlight=2)]
142142

143-
When `CreateDefaultBuilder` isn't called, add the user secrets configuration source explicitly by calling <xref:Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets%2A> in the `Startup` constructor. Call `AddUserSecrets` only when the app runs in the Development environment, as shown in the following example:
143+
When `CreateDefaultBuilder` isn't called, add the user secrets configuration source explicitly by calling <xref:Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets%2A>. Call `AddUserSecrets` only when the app runs in the Development environment, as shown in the following example:
144144

145-
[!code-csharp[](app-secrets/samples/3.x/UserSecrets/Startup2.cs?name=snippet_StartupConstructor&highlight=12)]
145+
[!code-csharp[](app-secrets/samples/3.x/UserSecrets/Program2.cs?name=snippet_Host&highlight=6-9)]
146146

147147
User secrets can be retrieved via the `Configuration` API:
148148

149-
[!code-csharp[](app-secrets/samples/2.x/UserSecrets/Startup.cs?name=snippet_StartupClass&highlight=14)]
150-
149+
[!code-csharp[](app-secrets/samples/3.x/UserSecrets/Startup.cs?name=snippet_StartupClass&highlight=14)]
151150

152151
## Map secrets to a POCO
153152

@@ -157,17 +156,17 @@ Mapping an entire object literal to a POCO (a simple .NET class with properties)
157156

158157
To map the preceding secrets to a POCO, use the `Configuration` API's [object graph binding](xref:fundamentals/configuration/index#bind-to-an-object-graph) feature. The following code binds to a custom `MovieSettings` POCO and accesses the `ServiceApiKey` property value:
159158

160-
[!code-csharp[](app-secrets/samples/2.x/UserSecrets/Startup3.cs?name=snippet_BindToObjectGraph)]
159+
[!code-csharp[](app-secrets/samples/3.x/UserSecrets/Startup3.cs?name=snippet_BindToObjectGraph)]
161160

162161
The `Movies:ConnectionString` and `Movies:ServiceApiKey` secrets are mapped to the respective properties in `MovieSettings`:
163162

164-
[!code-csharp[](app-secrets/samples/2.x/UserSecrets/Models/MovieSettings.cs?name=snippet_MovieSettingsClass)]
163+
[!code-csharp[](app-secrets/samples/3.x/UserSecrets/Models/MovieSettings.cs?name=snippet_MovieSettingsClass)]
165164

166165
## String replacement with secrets
167166

168167
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:
169168

170-
[!code-json[](app-secrets/samples/2.x/UserSecrets/appsettings-unsecure.json?highlight=3)]
169+
[!code-json[](app-secrets/samples/3.x/UserSecrets/appsettings-unsecure.json?highlight=3)]
171170

172171
A more secure approach is to store the password as a secret. For example:
173172

@@ -177,11 +176,11 @@ dotnet user-secrets set "DbPassword" "pass123"
177176

178177
Remove the `Password` key-value pair from the connection string in *appsettings.json*. For example:
179178

180-
[!code-json[](app-secrets/samples/2.x/UserSecrets/appsettings.json?highlight=3)]
179+
[!code-json[](app-secrets/samples/3.x/UserSecrets/appsettings.json?highlight=3)]
181180

182181
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:
183182

184-
[!code-csharp[](app-secrets/samples/2.x/UserSecrets/Startup2.cs?name=snippet_StartupClass&highlight=14-17)]
183+
[!code-csharp[](app-secrets/samples/3.x/UserSecrets/Startup2.cs?name=snippet_StartupClass&highlight=14-17)]
185184

186185
## List the secrets
187186

@@ -382,15 +381,13 @@ The [ASP.NET Core Configuration API](xref:fundamentals/configuration/index) prov
382381

383382
If your project targets .NET Framework, install the [Microsoft.Extensions.Configuration.UserSecrets](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.UserSecrets) NuGet package.
384383

385-
386384
In ASP.NET Core 2.0 or later, the user secrets configuration source is automatically added in development mode when the project calls <xref:Microsoft.AspNetCore.WebHost.CreateDefaultBuilder%2A> to initialize a new instance of the host with preconfigured defaults. `CreateDefaultBuilder` calls <xref:Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets%2A> when the <xref:Microsoft.AspNetCore.Hosting.IHostingEnvironment.EnvironmentName> is <xref:Microsoft.AspNetCore.Hosting.EnvironmentName.Development>:
387385

388386
[!code-csharp[](app-secrets/samples/2.x/UserSecrets/Program.cs?name=snippet_CreateWebHostBuilder&highlight=2)]
389387

390-
391388
When `CreateDefaultBuilder` isn't called, add the user secrets configuration source explicitly by calling <xref:Microsoft.Extensions.Configuration.UserSecretsConfigurationExtensions.AddUserSecrets%2A> in the `Startup` constructor. Call `AddUserSecrets` only when the app runs in the Development environment, as shown in the following example:
392389

393-
[!code-csharp[](app-secrets/samples/1.x/UserSecrets/Startup.cs?name=snippet_StartupConstructor&highlight=12)]
390+
[!code-csharp[](app-secrets/samples/2.x/UserSecrets/Startup3.cs?name=snippet_StartupConstructor&highlight=12)]
394391

395392
User secrets can be retrieved via the `Configuration` API:
396393

aspnetcore/security/app-secrets/samples/1.x/UserSecrets/Models/MovieSettings.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

aspnetcore/security/app-secrets/samples/1.x/UserSecrets/Program.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

aspnetcore/security/app-secrets/samples/1.x/UserSecrets/Startup.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

aspnetcore/security/app-secrets/samples/1.x/UserSecrets/Startup2.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.

aspnetcore/security/app-secrets/samples/1.x/UserSecrets/UserSecrets.csproj

Lines changed: 0 additions & 22 deletions
This file was deleted.

aspnetcore/security/app-secrets/samples/1.x/UserSecrets/appsettings-unsecure.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

aspnetcore/security/app-secrets/samples/1.x/UserSecrets/appsettings.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

aspnetcore/security/app-secrets/samples/2.x/UserSecrets/Startup3.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if never
22
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Http;
45
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.DependencyInjection;
@@ -11,10 +12,24 @@ public class Startup
1112
{
1213
private string _moviesApiKey = null;
1314

14-
public Startup(IConfiguration configuration)
15+
#region snippet_StartupConstructor
16+
public Startup(IHostingEnvironment env)
1517
{
16-
Configuration = configuration;
18+
var builder = new ConfigurationBuilder()
19+
.SetBasePath(env.ContentRootPath)
20+
.AddJsonFile("appsettings.json",
21+
optional: false,
22+
reloadOnChange: true)
23+
.AddEnvironmentVariables();
24+
25+
if (env.IsDevelopment())
26+
{
27+
builder.AddUserSecrets<Startup>();
28+
}
29+
30+
Configuration = builder.Build();
1731
}
32+
#endregion
1833

1934
public IConfiguration Configuration { get; }
2035

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#if never
2+
using Microsoft.AspNetCore.Hosting;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace UserSecrets
6+
{
7+
public class Program
8+
{
9+
public static void Main(string[] args)
10+
{
11+
#region snippet_Host
12+
var host = new HostBuilder()
13+
.ConfigureAppConfiguration((hostContext, builder) =>
14+
{
15+
// Add other providers for JSON, etc.
16+
17+
if (hostContext.HostingEnvironment.IsDevelopment())
18+
{
19+
builder.AddUserSecrets<Program>();
20+
}
21+
})
22+
.Build();
23+
#endregion
24+
25+
host.Run();
26+
}
27+
}
28+
}
29+
#endif

0 commit comments

Comments
 (0)