Skip to content

Commit 8658200

Browse files
authored
Update service-fabric-tutorial-dotnet-app-enable-https-endpoint.md
Some improvements regarding using self-signed development certificate: 1. Use `using` for creating a new store instead of manual closing it (as it's net core and we have a dispose method). 2. More accurate name for the method. 3. Throw an exception in case of missing the certificate. It will make the debugging easier. 4. Some note regarding the subject of a certificate for local deployment.
1 parent b9a72ac commit 8658200

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` server endpoint it's preferable to use the sunject "CN=localhost" (`Issued to` property of the certificate should contatin the url of the server endpoint) for avoiding 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)