Skip to content

Commit 69bec7d

Browse files
authored
Update Program.cs
1 parent 55aba25 commit 69bec7d

1 file changed

Lines changed: 42 additions & 48 deletions

File tree

  • aspnetcore/fundamentals/servers/kestrel/samples/6.x/KestrelSample/Snippets

aspnetcore/fundamentals/servers/kestrel/samples/6.x/KestrelSample/Snippets/Program.cs

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -135,36 +135,33 @@ public static void ServerOptionsSelectionCallback(string[] args)
135135
{
136136
serverOptions.ListenAnyIP(5005, listenOptions =>
137137
{
138-
listenOptions.UseHttps(httpsOptions =>
138+
var localhostCert = CertificateLoader.LoadFromStoreCert(
139+
"localhost", "My", StoreLocation.CurrentUser,
140+
allowInvalid: true);
141+
var exampleCert = CertificateLoader.LoadFromStoreCert(
142+
"example.com", "My", StoreLocation.CurrentUser,
143+
allowInvalid: true);
144+
145+
listenOptions.UseHttps((stream, clientHelloInfo, state, cancellationToken) =>
139146
{
140-
var localhostCert = CertificateLoader.LoadFromStoreCert(
141-
"localhost", "My", StoreLocation.CurrentUser,
142-
allowInvalid: true);
143-
var exampleCert = CertificateLoader.LoadFromStoreCert(
144-
"example.com", "My", StoreLocation.CurrentUser,
145-
allowInvalid: true);
146-
147-
listenOptions.UseHttps((stream, clientHelloInfo, state, cancellationToken) =>
147+
if (string.Equals(clientHelloInfo.ServerName, "localhost",
148+
StringComparison.OrdinalIgnoreCase))
148149
{
149-
if (string.Equals(clientHelloInfo.ServerName, "localhost",
150-
StringComparison.OrdinalIgnoreCase))
151-
{
152-
return new ValueTask<SslServerAuthenticationOptions>(
153-
new SslServerAuthenticationOptions
154-
{
155-
ServerCertificate = localhostCert,
156-
// Different TLS requirements for this host
157-
ClientCertificateRequired = true
158-
});
159-
}
160-
161150
return new ValueTask<SslServerAuthenticationOptions>(
162151
new SslServerAuthenticationOptions
163152
{
164-
ServerCertificate = exampleCert
153+
ServerCertificate = localhostCert,
154+
// Different TLS requirements for this host
155+
ClientCertificateRequired = true
165156
});
166-
}, state: null!);
167-
});
157+
}
158+
159+
return new ValueTask<SslServerAuthenticationOptions>(
160+
new SslServerAuthenticationOptions
161+
{
162+
ServerCertificate = exampleCert
163+
});
164+
}, state: null!);
168165
});
169166
});
170167
// </snippet_ServerOptionsSelectionCallback>
@@ -179,39 +176,36 @@ public static void TlsHandshakeCallbackOptions(string[] args)
179176
{
180177
serverOptions.ListenAnyIP(5005, listenOptions =>
181178
{
182-
listenOptions.UseHttps(httpsOptions =>
179+
var localhostCert = CertificateLoader.LoadFromStoreCert(
180+
"localhost", "My", StoreLocation.CurrentUser,
181+
allowInvalid: true);
182+
var exampleCert = CertificateLoader.LoadFromStoreCert(
183+
"example.com", "My", StoreLocation.CurrentUser,
184+
allowInvalid: true);
185+
186+
listenOptions.UseHttps(new TlsHandshakeCallbackOptions
183187
{
184-
var localhostCert = CertificateLoader.LoadFromStoreCert(
185-
"localhost", "My", StoreLocation.CurrentUser,
186-
allowInvalid: true);
187-
var exampleCert = CertificateLoader.LoadFromStoreCert(
188-
"example.com", "My", StoreLocation.CurrentUser,
189-
allowInvalid: true);
190-
191-
listenOptions.UseHttps(new TlsHandshakeCallbackOptions
188+
OnConnection = context =>
192189
{
193-
OnConnection = context =>
190+
if (string.Equals(context.ClientHelloInfo.ServerName, "localhost",
191+
StringComparison.OrdinalIgnoreCase))
194192
{
195-
if (string.Equals(context.ClientHelloInfo.ServerName, "localhost",
196-
StringComparison.OrdinalIgnoreCase))
197-
{
198-
// Different TLS requirements for this host
199-
context.AllowDelayedClientCertificateNegotation = true;
200-
201-
return new ValueTask<SslServerAuthenticationOptions>(
202-
new SslServerAuthenticationOptions
203-
{
204-
ServerCertificate = localhostCert
205-
});
206-
}
193+
// Different TLS requirements for this host
194+
context.AllowDelayedClientCertificateNegotation = true;
207195

208196
return new ValueTask<SslServerAuthenticationOptions>(
209197
new SslServerAuthenticationOptions
210198
{
211-
ServerCertificate = exampleCert
199+
ServerCertificate = localhostCert
212200
});
213201
}
214-
});
202+
203+
return new ValueTask<SslServerAuthenticationOptions>(
204+
new SslServerAuthenticationOptions
205+
{
206+
ServerCertificate = exampleCert
207+
});
208+
}
215209
});
216210
});
217211
});

0 commit comments

Comments
 (0)