Skip to content

Commit d2a2814

Browse files
authored
Merge pull request #100972 from MicrosoftDocs/release-ga-immersive-reader
Release ga immersive reader
2 parents 71df2d1 + befc03f commit d2a2814

34 files changed

+1563
-460
lines changed

articles/cognitive-services/immersive-reader/azure-active-directory-authentication.md

Lines changed: 0 additions & 136 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: "Cache the authentication token"
3+
titleSuffix: Azure Cognitive Services
4+
description: This article will show you how to cache the authentication token.
5+
author: metanMSFT
6+
manager: guillasi
7+
8+
ms.service: cognitive-services
9+
ms.subservice: immersive-reader
10+
ms.topic: conceptual
11+
ms.date: 01/14/2020
12+
ms.author: metan
13+
---
14+
15+
# How to cache the authentication token
16+
17+
This article demonstrates how to cache the authentication token in order to improve performance of your application.
18+
19+
## Using ASP.NET
20+
21+
Import the **Microsoft.IdentityModel.Clients.ActiveDirectory** NuGet package, which is used to acquire a token. Next, use the following code to acquire an `AuthenticationResult`, using the authentication values you got when you [created the Immersive Reader resource](./how-to-create-immersive-reader.md).
22+
23+
```csharp
24+
private async Task<AuthenticationResult> GetTokenAsync()
25+
{
26+
AuthenticationContext authContext = new AuthenticationContext($"https://login.windows.net/{TENANT_ID}");
27+
ClientCredential clientCredential = new ClientCredential(CLIENT_ID, CLIENT_SECRET);
28+
AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://cognitiveservices.azure.com/", clientCredential);
29+
return authResult;
30+
}
31+
```
32+
33+
The `AuthenticationResult` object has an `AccessToken` property which is the actual token you will use when launching the Immersive Reader using the SDK. It also has an `ExpiresOn` property which denotes when the token will expire. Before launching the Immersive Reader, you can check whether the token has expired, and acquire a new token only if it has expired.
34+
35+
## Using Node.JS
36+
37+
Add the [**request**](https://www.npmjs.com/package/request) npm package to your project. Use the following code to acquire a token, using the authentication values you got when you [created the Immersive Reader resource](./how-to-create-immersive-reader.md).
38+
39+
```javascript
40+
router.get('/token', function(req, res) {
41+
request.post(
42+
{
43+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
44+
url: `https://login.windows.net/${TENANT_ID}/oauth2/token`,
45+
form: {
46+
grant_type: 'client_credentials',
47+
client_id: CLIENT_ID,
48+
client_secret: CLIENT_SECRET,
49+
resource: 'https://cognitiveservices.azure.com/'
50+
}
51+
},
52+
function(err, resp, json) {
53+
const result = JSON.parse(json);
54+
return res.send({
55+
access_token: result.access_token,
56+
expires_on: result.expires_on
57+
});
58+
}
59+
);
60+
});
61+
```
62+
63+
The `expires_on` property is the date and time at which the token expires, expressed as the number of seconds since January 1, 1970 UTC. Use this value to determine whether your token has expired before attempting to acquire a new one.
64+
65+
```javascript
66+
async function getToken() {
67+
if (Date.now() / 1000 > CREDENTIALS.expires_on) {
68+
CREDENTIALS = await refreshCredentials();
69+
}
70+
return CREDENTIALS.access_token;
71+
}
72+
```
73+
74+
## Next steps
75+
76+
* Explore the [Immersive Reader SDK Reference](./reference.md)

0 commit comments

Comments
 (0)