Skip to content

Commit b5952eb

Browse files
authored
Update configure-ssl-certificate-in-code.md
1 parent a7fe60f commit b5952eb

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

articles/app-service/configure-ssl-certificate-in-code.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,29 @@ In C# code, you access the certificate by the certificate thumbprint. The follow
5454

5555
```csharp
5656
using System;
57+
using System.Linq;
5758
using System.Security.Cryptography.X509Certificates;
5859

59-
...
60-
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
61-
certStore.Open(OpenFlags.ReadOnly);
62-
X509Certificate2Collection certCollection = certStore.Certificates.Find(
63-
X509FindType.FindByThumbprint,
64-
// Replace below with your certificate's thumbprint
65-
"E661583E8FABEF4C0BEF694CBC41C28FB81CD870",
66-
false);
67-
// Get the first cert with the thumbprint
68-
if (certCollection.Count > 0)
60+
string certThumbprint = "E661583E8FABEF4C0BEF694CBC41C28FB81CD870";
61+
62+
using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
6963
{
70-
X509Certificate2 cert = certCollection[0];
71-
// Use certificate
72-
Console.WriteLine(cert.FriendlyName);
64+
certStore.Open(OpenFlags.ReadOnly);
65+
66+
X509Certificate2Collection certCollection = certStore.Certificates.Find(
67+
X509FindType.FindByThumbprint,
68+
// Replace below with your certificate's thumbprint
69+
certThumbprint,
70+
false);
71+
// Get the first cert with the thumbprint
72+
X509Certificate2 cert = certCollection.OfType<X509Certificate>().FirstOrDefault();
73+
74+
if (cert is null)
75+
throw new Exception($"Certificate with thumbprint {certThumbprint} was not found");
76+
77+
// Use certificate
78+
Console.WriteLine(cert.FriendlyName);
7379
}
74-
certStore.Close();
75-
...
7680
```
7781

7882
In Java code, you access the certificate from the "Windows-MY" store using the Subject Common Name field (see [Public key certificate](https://en.wikipedia.org/wiki/Public_key_certificate)). The following code shows how to load a private key certificate:

0 commit comments

Comments
 (0)