Skip to content

Commit 5afeff9

Browse files
authored
Merge pull request #89722 from Nickomang/dotnethttpsupdate
Dotnet Updates for API Management
2 parents 6ea8a03 + 810ffcc commit 5afeff9

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

articles/service-fabric/service-fabric-tutorial-dotnet-app-enable-https-endpoint.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ serviceContext =>
135135
int port = serviceContext.CodePackageActivationContext.GetEndpoint("EndpointHttps").Port;
136136
opt.Listen(IPAddress.IPv6Any, port, listenOptions =>
137137
{
138-
listenOptions.UseHttps(GetCertificateFromStore());
138+
listenOptions.UseHttps(GetHttpsCertificateFromStore());
139139
listenOptions.NoDelay = true;
140140
});
141141
})
@@ -160,21 +160,23 @@ serviceContext =>
160160
Also add the following method so that Kestrel can find the certificate in the `Cert:\LocalMachine\My` store using the subject.
161161

162162
Replace "<your_CN_value>" with "mytestcert" if you created a self-signed certificate with the previous PowerShell command, or use the CN of your certificate.
163+
Be aware that in the case of local deployment to `localhost` it's preferable to use "CN=localhost" to avoid authentication exceptions.
163164

164165
```csharp
165-
private X509Certificate2 GetCertificateFromStore()
166+
private X509Certificate2 GetHttpsCertificateFromStore()
166167
{
167-
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
168-
try
168+
using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
169169
{
170170
store.Open(OpenFlags.ReadOnly);
171171
var certCollection = store.Certificates;
172172
var currentCerts = certCollection.Find(X509FindType.FindBySubjectDistinguishedName, "CN=<your_CN_value>", false);
173-
return currentCerts.Count == 0 ? null : currentCerts[0];
174-
}
175-
finally
176-
{
177-
store.Close();
173+
174+
if (currentCerts.Count == 0)
175+
{
176+
throw new Exception("Https certificate is not found.");
177+
}
178+
179+
return currentCerts[0];
178180
}
179181
}
180182
```

0 commit comments

Comments
 (0)