Skip to content

Commit 03e0eaa

Browse files
Merge pull request #51598 from abatishchev/patch-1
Update configure-ssl-certificate-in-code.md
2 parents 067c696 + b3cb6e3 commit 03e0eaa

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,32 @@ 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+
bool validOnly = false;
62+
63+
using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
6964
{
70-
X509Certificate2 cert = certCollection[0];
71-
// Use certificate
72-
Console.WriteLine(cert.FriendlyName);
65+
certStore.Open(OpenFlags.ReadOnly);
66+
67+
X509Certificate2Collection certCollection = certStore.Certificates.Find(
68+
X509FindType.FindByThumbprint,
69+
// Replace below with your certificate's thumbprint
70+
certThumbprint,
71+
validOnly);
72+
// Get the first cert with the thumbprint
73+
X509Certificate2 cert = certCollection.OfType<X509Certificate>().FirstOrDefault();
74+
75+
if (cert is null)
76+
throw new Exception($"Certificate with thumbprint {certThumbprint} was not found");
77+
78+
// Use certificate
79+
Console.WriteLine(cert.FriendlyName);
80+
81+
// Consider to call Dispose() on the certificate after it's being used, avaliable in .NET 4.6 and later
7382
}
74-
certStore.Close();
75-
...
7683
```
7784

7885
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:
@@ -107,10 +114,11 @@ The certificate file names are the certificate thumbprints. The following C# cod
107114

108115
```csharp
109116
using System;
117+
using System.IO;
110118
using System.Security.Cryptography.X509Certificates;
111119

112120
...
113-
var bytes = System.IO.File.ReadAllBytes("/var/ssl/certs/<thumbprint>.der");
121+
var bytes = File.ReadAllBytes("/var/ssl/certs/<thumbprint>.der");
114122
var cert = new X509Certificate2(bytes);
115123

116124
// Use the loaded certificate
@@ -135,10 +143,11 @@ The following C# example loads a public certificate from a relative path in your
135143
136144
```csharp
137145
using System;
146+
using System.IO;
138147
using System.Security.Cryptography.X509Certificates;
139148
140149
...
141-
var bytes = System.IO.File.ReadAllBytes("~/<relative-path-to-cert-file>");
150+
var bytes = File.ReadAllBytes("~/<relative-path-to-cert-file>");
142151
var cert = new X509Certificate2(bytes);
143152
144153
// Use the loaded certificate

0 commit comments

Comments
 (0)