Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit 46f001c

Browse files
authored
[JS] Add details to README in certificate samples (#3994)
* improve certificate authentication sample * add details to SNI sample * format code * Reordering and Formatting * include MicrosoftAppTenantId * include SN+I details * add MicrosoftAppType
1 parent 9b7cac0 commit 46f001c

37 files changed

Lines changed: 334 additions & 114 deletions

samples/javascript_nodejs/84.bot-authentication-certificate/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
MicrosoftAppTenantId=
2+
MicrosoftAppType=
23
MicrosoftAppId=
34
CertificateThumbprint=
45
KeyVaultName=
Lines changed: 164 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1+
[#LocalEnv]:#using-local-environment
2+
[#KeyVaultEnv]:#using-keyvault
3+
[#TrySample]:#to-try-this-sample
4+
15
# Authentication Bot using SSL/TLS certificates
26

37
Bot Framework v4 bot authentication using Certificate
48

5-
This bot has been created using [Bot Framework](https://dev.botframework.com/), is shows how to use the bot authentication capabilities of Azure Bot Service. In this sample, we use a local or KeyVault certificate to create the Bot Framework Authentication.
9+
This bot has been created using [Bot Framework](https://dev.botframework.com/), it shows how to use the bot authentication capabilities of Azure Bot Service.
10+
11+
In this guide, we'll explain how to create and consume a certificate in Bot Framework with the following options:
12+
1. [Local environment][#LocalEnv]
13+
2. [KeyVault][#KeyVaultEnv]
14+
15+
## Interacting with the bot
16+
17+
This sample uses the bot authentication capabilities of Azure Bot Service, providing features to make it easier to develop a bot that authenticates users using digital security certificates. You just need to provide the certificate data linked to the managed identity and run the bot, then communicate with it to validate its correct authentication.
18+
19+
## SSL/TLS certificate
20+
21+
An SSL/TLS certificate is a digital object that allows systems to verify identity and subsequently establish an encrypted network connection with another system using the Secure Sockets Layer/Transport Layer Security (SSL/TLS) protocol. Certificates are issued using a cryptographic system known as public key infrastructure (PKI). PKI allows one party to establish the identity of another through the use of certificates if they both trust a third party, known as a certificate authority. SSL/TLS certificates therefore function as digital identity documents that protect network communications and establish the identity of websites on the Internet as well as resources on private networks.
622

723
## Prerequisites
824

9-
- [Node.js](https://nodejs.org) version 18 or higher
25+
- [Ngrok](https://ngrok.com/) latest version.
26+
- [Node.js](https://nodejs.org) version 16.16.0 or higher
1027

1128
```bash
1229
# determine node version
@@ -15,49 +32,167 @@ node --version
1532

1633
## To try this sample
1734

35+
- Setup ngrok
36+
1. Follow this [guide](https://ngrok.com/docs/getting-started/?os=windows) to install and configure ngrok in your environment.
37+
2. Run ngrok with the following command.
38+
```bash
39+
ngrok http --host-header=rewrite 3978
40+
```
41+
42+
- Setup a Bot
43+
1. Register a bot with Azure Bot Service, following the instructions [here](https://docs.microsoft.com/azure/bot-service/bot-service-quickstart-registration?view=azure-bot-service-3.0).
44+
45+
2. After registering the bot, use `<NGROK_FORWARDING_DOMAIN>/api/messages` as the messaging endpoint.
46+
> NOTE: make sure to take note of the Microsoft App Id as we'll need this for later.
47+
1848
- Clone the repository
1949
20-
```bash
21-
git clone https://github.com/microsoft/botbuilder-samples.git
22-
```
50+
```bash
51+
git clone https://github.com/microsoft/botbuilder-samples.git
52+
```
2353
24-
- In a terminal, navigate to `samples/javascript_nodejs/84.bot-authentication-certificate`
54+
- Create and configure the SSL/TSL certificate. In this sample we use two possible options to create and set an SSL/TSL certificate. Below is a step-by-step description of each one:
2555
26-
```bash
27-
cd samples/javascript_nodejs/84.bot-authentication-certificate
28-
```
56+
### Using local environment
57+
1. Configure the following app settings variables:
58+
- MicrosoftAppId: App Id of your bot (gathered from the [Setup a Bot][#TrySample] step).
59+
- CertificateThumbprint: Thumbprint of the certificate uploaded to the app registration.
60+
- MicrosoftAppTenantId: Tenant Id to which your bot belongs (optional for MultiTenant apps).
61+
- MicrosoftAppType: Type of the App (optional for MultiTenant apps).
2962
30-
- Install modules
63+
2. Install and configure [OpenSSL](https://www.openssl.org/source/) with the latest version
64+
- Download the latest version source and add the folder to the [environment variables](https://www.java.com/en/download/help/path.html) path.
65+
```bash
66+
setx path "%path%;<OpenSSL path here>
67+
e.g
68+
setx path "%path%;C:\Program Files\openssl-3.3.0"
69+
```
3170
32-
```bash
33-
npm install
34-
```
71+
3. Run the following command in PowerShell
72+
- For **global environment certificate** execute the following command with **admin privileges**:
73+
```powershell
74+
$cert = New-SelfSignedCertificate -CertStoreLocation "." -Subject "CN=<certificate-name>" -KeySpec KeyExchange
75+
```
76+
e.g:
77+
![Global Certificate Command](images/local/GlobalCertificateCommand.png)
3578
36-
- Start the bot
79+
- For **current user certificate** execute:
80+
```powershell
81+
$cert = New-SelfSignedCertificate -CertStoreLocation "Cert:\CurrentUser\My" -Subject "CN=<certificate-name>" -KeySpec KeyExchange
82+
```
83+
e.g:
84+
![User Certificate Command](images/local/UserCertificateCommand.png)
3785
38-
```bash
39-
npm start
40-
```
86+
4. Then, type _Manage computer certificates(global environment certificate)_ or _Manage User Certificates(current user certificate)_ in the Windows search bar and hit enter.
4187
42-
## Testing the bot using Bot Framework Emulator
88+
![User Certificate Search](images/local/CertificateSearch.png)
4389
44-
[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.
90+
5. The certificate will be located in the _user certificates_ folder, under _personal_ directory.
4591
46-
- Install the latest Bot Framework Emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)
92+
![Certificate Directory](images/local/CertificateDirectory.png)
4793
48-
### Connect to the bot using Bot Framework Emulator
94+
6. Export the certificate to _pfx_ format including the key.
4995
50-
- Launch Bot Framework Emulator
51-
- File -> Open Bot
52-
- Enter a Bot URL of `http://localhost:3978/api/messages`
96+
![Certificate Export Steps](images/local/CertificateExportSteps1.png)
97+
![Certificate Export Steps](images/local/CertificateExportSteps2.png)
5398
54-
## Interacting with the bot
99+
7. Go to the certificate location and run the following command to generate a _pem_ file (the command will ask for the password generated in the previous step):
55100
56-
This sample uses the bot authentication capabilities of Azure Bot Service, providing features to make it easier to develop a bot that authenticates users using digital security certificates. You just need to provide the certificate data linked to the managed identity and run the bot, then communicate with it to validate its correct authentication.
101+
```powershell
102+
OpenSSL pkcs12 -in .\<certificate-name>.pfx -out <certificate-name>.pem �nodes -nokeys
103+
```
104+
e.g:
105+
![Pem File Command No Key](images/local/PemCommandNoKey.png)
57106
58-
## SSL/TLS certificate
107+
8. Upload the generated certificate to the Azure app registration. Once uploaded, save the thumbprint value showed in the list of certificates.
59108
60-
An SSL/TLS certificate is a digital object that allows systems to verify identity and subsequently establish an encrypted network connection with another system using the Secure Sockets Layer/Transport Layer Security (SSL/TLS) protocol. Certificates are issued using a cryptographic system known as public key infrastructure (PKI). PKI allows one party to establish the identity of another through the use of certificates if they both trust a third party, known as a certificate authority. SSL/TLS certificates therefore function as digital identity documents that protect network communications and establish the identity of websites on the Internet as well as resources on private networks.
109+
![Certificate Upload](images/local/CertificateUpload.png)
110+
111+
9. To read the certificate in the bot, the _pem_ file must include the key, then go to the certificate location and run the following command to generate a _pem_ file with key:
112+
```powershell
113+
OpenSSL pkcs12 -in .\<certificate-name>.pfx -out <certificate-with-key-name>.pem �nodes
114+
```
115+
116+
![Pem Command With Key](images/local/PemCommandWithKey.png)
117+
118+
10. In the sample code, go to the [index](index.js) file and uncomment the line of code that reads the local certificate and write the name of the certificate in _pem_ format inside the _CreateFromPemFile_ method.
119+
Be sure to comment out or remove the lines of code that use Azure KeyVault to avoid errors.
120+
> NOTE: Here the value of `MicrosoftAppId` and `CertificateThumbprint` are needed to generate the credentials.
121+
122+
![Certificate Reading](images/local/CertificateReading.png)
123+
124+
### Using KeyVault
125+
1. This option requires the following app settings variables:
126+
- KeyVaultName: Name of the KeyVault containing the certificate.
127+
- CertificateName: Name of the certificate in the KeyVault.
128+
- MicrosoftAppId: App Id of your bot (gathered from the [Setup a Bot][#TrySample] step).
129+
- CertificateThumbprint: Thumbprint of the certificate uploaded to the app registration.
130+
- MicrosoftAppTenantId: Tenant Id to which your bot belongs (optional for MultiTenant apps).
131+
- MicrosoftAppType: Type of the App (optional for MultiTenant apps).
132+
133+
2. Create a [KeyVault](https://learn.microsoft.com/en-us/azure/key-vault/general/quick-create-portal) resource.
134+
135+
3. Assign KeyVault [permissions](https://learn.microsoft.com/en-us/azure/key-vault/general/rbac-guide?tabs=azure-cli) to the current user if needed to create a new certificate.
136+
137+
4. Under the Certificates section, hit on Generate/Import, complete the form, and create the certificate in _pem_ format.
138+
139+
![Generate Certificate](images/keyVault/GenerateCertificate.png)
140+
![Create Certificate](images/keyVault/CreateCertificate.png)
141+
142+
5. Go to the details of the certificate and download it in _CER_ format to avoid the export of the private key.
143+
144+
![Certificate Details](images/keyVault/CertificateDetails.png)
145+
![Download Certificate](images/keyVault/DownloadCertificate.png)
146+
147+
>NOTE: If you downloaded it in _PEM_ format, it will be neccesary to remove the private key by executing the following command:
148+
```
149+
OpenSSL pkcs12 -in .\<certificate-name>.pem -export -out .\<certificate-without-key-name>.pem -nokeys
150+
```
151+
e.g:
152+
![Remove Keys](images/keyVault/RemoveKeys.png)
153+
154+
>NOTE: If you used _pkcs_ format in the creation step and downloaded it in _PFX_ format, install OpenSSL and follow the step 6 of the previous [section](#using-local-environment) to convert it to _pem_ format without keys.
155+
156+
6. Upload the certificate to the Azure app registration.
157+
158+
![Upload Cer Certificate](images/keyVault/UploadCerCertificate.png)
159+
160+
7. In the sample code, go to the [Startup](Startup.cs) class and uncomment the line of code that reads the keyvault certificate and verify that the keyvault credentials are completed in the [.env](.env) file.
161+
Be sure to comment out or remove the lines of code that use local certificate to avoid errors.
162+
> NOTE: Here the value of `MicrosoftAppId` and `CertificateThumbprint` are also needed to generate the credentials.
163+
164+
![Certificate Reading](images/keyVault/CertificateReading.png)
165+
166+
8. In the current sample context, log into Azure to obtain the default credentials by executing the following command.
167+
```powershell
168+
az login
169+
```
170+
171+
### Run the bot from a terminal or from Visual Studio:
172+
173+
- In a terminal, navigate to `samples/javascript_nodejs/84.bot-authentication-certificate`
174+
175+
```bash
176+
cd samples/javascript_nodejs/84.bot-authentication-certificate
177+
```
178+
179+
- Install modules
180+
181+
```bash
182+
npm install
183+
```
184+
185+
- Start the bot
186+
187+
```bash
188+
npm start
189+
```
190+
191+
## Testing the bot using Azure Bot
192+
193+
Go to the Azure bot resource created previously, select the _Test in Web Chat_ option under the _Settings_ section and start talking to the bot.
194+
195+
![Bot Conversation](images/BotConversation.png)
61196
62197
## Deploy the bot to Azure
63198
@@ -84,5 +219,5 @@ To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](htt
84219
- [Restify](https://www.npmjs.com/package/restify)
85220
86221
- [dotenv](https://www.npmjs.com/package/dotenv)
87-
222+
88223
- [SSL/TLS certificates](https://www.digicert.com/tls-ssl/tls-ssl-certificates)
18.6 KB
Loading
105 KB
Loading
76.6 KB
Loading
38.5 KB
Loading
45.7 KB
Loading
46.1 KB
Loading
5.51 KB
Loading
200 KB
Loading

0 commit comments

Comments
 (0)