You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-daemon-acquire-token.md
+77-32Lines changed: 77 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,11 +22,23 @@ After you've constructed a confidential client application, you can acquire a to
22
22
23
23
The scope to request for a client credential flow is the name of the resource followed by `/.default`. This notation tells Azure Active Directory (Azure AD) to use the *application-level permissions* declared statically during application registration. Also, these API permissions must be granted by a tenant administrator.
24
24
25
-
# [.NET](#tab/dotnet)
25
+
# [.NET](#tab/idweb)
26
26
27
-
```csharp
28
-
ResourceId="someAppIDURI";
29
-
varscopes=new [] { ResourceId+"/.default"};
27
+
Here's an example of defining the scopes for the web API as part of the configuration in an [*appsettings.json*](https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2/blob/master/2-Call-OwnApi/daemon-console/appsettings.json) file. This example is taken from the [.NET Core console daemon](https://github.com/Azure-Samples/active-directory-dotnetcore-daemon-v2) code sample on GitHub.
28
+
29
+
```json
30
+
{
31
+
"AzureAd": {
32
+
// Same AzureAd section as before.
33
+
},
34
+
35
+
"MyWebApi": {
36
+
"BaseUrl": "https://localhost:44372/",
37
+
"RelativePath": "api/TodoList",
38
+
"RequestAppToken": true,
39
+
"Scopes": [ "[Enter here the scopes for your web API]" ]
40
+
}
41
+
}
30
42
```
31
43
32
44
# [Java](#tab/java)
@@ -53,6 +65,13 @@ In MSAL Python, the configuration file looks like this code snippet:
53
65
}
54
66
```
55
67
68
+
# [.NET (low level)](#tab/dotnet)
69
+
70
+
```csharp
71
+
ResourceId="someAppIDURI";
72
+
varscopes=new [] { ResourceId+"/.default"};
73
+
```
74
+
56
75
---
57
76
58
77
### Azure AD (v1.0) resources
@@ -65,41 +84,29 @@ The scope used for client credentials should always be the resource ID followed
65
84
66
85
## AcquireTokenForClient API
67
86
68
-
To acquire a token for the app, you'll use `AcquireTokenForClient` or its equivalent, depending on the platform.
87
+
To acquire a token for the app, use `AcquireTokenForClient` or its equivalent, depending on the platform.
69
88
70
-
# [.NET](#tab/dotnet)
89
+
# [.NET](#tab/idweb)
90
+
91
+
With Microsoft.Identity.Web, you don't need to acquire a token. You can use higher level APIs, as you see in [Calling a web API from a daemon application](scenario-daemon-call-api.md). If however you're using an SDK that requires a token, the following code snippet shows how to get this token.
71
92
72
93
```csharp
73
-
usingMicrosoft.Identity.Client;
94
+
usingMicrosoft.Extensions.DependencyInjection;
95
+
usingMicrosoft.Identity.Abstractions;
96
+
usingMicrosoft.Identity.Web;
74
97
75
-
// With client credentials flows, the scope is always of the shape "resource/.default" because the
76
-
// application permissions need to be set statically (in the portal or by PowerShell), and then granted by
Don't call `AcquireTokenSilent` before you call `AcquireTokenForClient`, because `AcquireTokenSilent` uses the *user* token cache. `AcquireTokenForClient` checks the *application* token cache itself and updates it.
109
+
Don't call `AcquireTokenSilent` before you call `AcquireTokenForClient` because `AcquireTokenSilent` uses the *user* token cache. `AcquireTokenForClient` checks the *application* token cache itself and updates it.
Don't call `AcquireTokenSilent` before you call `AcquireTokenForClient`, because `AcquireTokenSilent` uses the *user* token cache. `AcquireTokenForClient` checks the *application* token cache itself and updates it.
230
+
190
231
---
191
232
192
233
### Protocol
@@ -253,10 +294,10 @@ If your daemon app calls your own web API and you weren't able to add an app per
253
294
254
295
## Next steps
255
296
256
-
# [.NET](#tab/dotnet)
297
+
# [.NET](#tab/idweb)
257
298
258
299
Moveontothenextarticleinthisscenario,
259
-
[Calling a web API](./scenario-daemon-call-api.md?tabs=dotnet).
Copy file name to clipboardExpand all lines: articles/active-directory/develop/scenario-daemon-call-api.md
+54-7Lines changed: 54 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,15 +20,53 @@ ms.custom: aaddev
20
20
21
21
# Daemon app that calls web APIs - call a web API from the app
22
22
23
-
.NET daemon apps can call a web API. .NET daemon apps can also call several pre-approved web APIs.
23
+
.NET daemon apps can call a web API. .NET daemon apps can also call several preapproved web APIs.
24
24
25
25
## Calling a web API from a daemon application
26
26
27
27
Here's how to use the token to call an API:
28
28
29
-
# [.NET](#tab/dotnet)
29
+
# [.NET](#tab/idweb)
30
30
31
-
[!INCLUDE [Call web API in .NET](../../../includes/active-directory-develop-scenarios-call-apis-dotnet.md)]
31
+
Microsoft.Identity.Web abstracts away the complexity of MSAL.NET. It provides you with higher-level APIs that handle the internals of MSAL.NET for you, such as processing Conditional Access errors, caching.
32
+
33
+
Here's the Program.cs of the daemon app calling a downstream API:
34
+
35
+
```csharp
36
+
usingMicrosoft.Extensions.DependencyInjection;
37
+
usingMicrosoft.Identity.Abstractions;
38
+
usingMicrosoft.Identity.Web;
39
+
40
+
// In the Program.cs, acquire a token for your downstream API
data = requests.get(endpoint, headers=http_headers, stream=False).json()
87
125
```
88
126
127
+
# [.NET low level](#tab/dotnet)
128
+
129
+
[!INCLUDE [Call web API in .NET](../../../includes/active-directory-develop-scenarios-call-apis-dotnet.md)]
130
+
89
131
---
90
132
91
133
## Calling several APIs
92
134
93
-
For daemon apps, the web APIs that you call need to be pre-approved. There's no incremental consent with daemon apps. (There's no user interaction.) The tenant admin needs to provide consent in advance for the application and all the API permissions. If you want to call several APIs, acquire a token for each resource, each time calling `AcquireTokenForClient`. MSAL will use the application token cache to avoid unnecessary service calls.
135
+
For daemon apps, the web APIs that you call need to be preapproved. There's no incremental consent with daemon apps. (There's no user interaction.) The tenant admin needs to provide consent in advance for the application and all the API permissions. If you want to call several APIs, acquire a token for each resource, each time calling `AcquireTokenForClient`. MSAL uses the application token cache to avoid unnecessary service calls.
94
136
95
137
## Next steps
96
138
97
-
# [.NET](#tab/dotnet)
139
+
# [.NET](#tab/idweb)
98
140
99
141
Move on to the next article in this scenario,
100
-
[Move to production](./scenario-daemon-production.md?tabs=dotnet).
142
+
[Move to production](./scenario-daemon-production.md?tabs=idweb).
101
143
102
144
# [Java](#tab/java)
103
145
@@ -114,4 +156,9 @@ Move on to the next article in this scenario,
114
156
Move on to the next article in this scenario,
115
157
[Move to production](./scenario-daemon-production.md?tabs=python).
116
158
117
-
---
159
+
# [.NET low level](#tab/dotnet)
160
+
161
+
Move on to the next article in this scenario,
162
+
[Move to production](./scenario-daemon-production.md?tabs=dotnet).
0 commit comments