Skip to content

Commit 0d568e2

Browse files
Copilotjmprieur
andcommitted
Update README.AgentIdentities.md to document OID-based overload
Co-authored-by: jmprieur <[email protected]>
1 parent 4c5ef19 commit 0d568e2

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/Microsoft.Identity.Web.AgentIdentities/README.AgentIdentities.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,16 @@ string authHeader = await authorizationHeaderProvider
151151

152152
#### Agent User Identity
153153

154-
For your agent application to acquire tokens on behalf of a agent user identity:
154+
For your agent application to acquire tokens on behalf of a agent user identity, you can use either the user's UPN (User Principal Name) or OID (Object ID).
155+
156+
##### Using UPN (User Principal Name)
155157

156158
```csharp
157159
// Get the required services
158160
IAuthorizationHeaderProvider authorizationHeaderProvider =
159161
serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>();
160162

161-
// Configure options for the agent user identity
163+
// Configure options for the agent user identity using UPN
162164
string agentIdentity = "agent-identity-client-id";
163165
string userUpn = "[email protected]";
164166
var options = new AuthorizationHeaderProviderOptions()
@@ -178,6 +180,33 @@ string authHeader = await authorizationHeaderProvider
178180
// in another call it will use the cached token.
179181
```
180182

183+
##### Using OID (Object ID)
184+
185+
```csharp
186+
// Get the required services
187+
IAuthorizationHeaderProvider authorizationHeaderProvider =
188+
serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>();
189+
190+
// Configure options for the agent user identity using OID
191+
string agentIdentity = "agent-identity-client-id";
192+
Guid userOid = Guid.Parse("e1f76997-1b35-4aa8-8a58-a5d8f1ac4636");
193+
var options = new AuthorizationHeaderProviderOptions()
194+
.WithAgentUserIdentity(agentIdentity, userOid);
195+
196+
// Create a ClaimsPrincipal to enable token caching
197+
ClaimsPrincipal user = new ClaimsPrincipal();
198+
199+
// Acquire a user token
200+
string authHeader = await authorizationHeaderProvider
201+
.CreateAuthorizationHeaderForUserAsync(
202+
scopes: ["https://graph.microsoft.com/.default"],
203+
options: options,
204+
user: user);
205+
206+
// The user object now has claims including uid and utid. If you use it
207+
// in another call it will use the cached token.
208+
```
209+
181210
### 4. Microsoft Graph Integration
182211

183212
Install the Microsoft.Identity.Web.GraphServiceClient which handles authentication for the Graph SDK
@@ -211,14 +240,21 @@ var applications = await graphServiceClient.Applications
211240

212241
#### Using Agent User Identity with Microsoft Graph:
213242

243+
You can use either UPN or OID with Microsoft Graph:
244+
214245
```csharp
215246
// Get the GraphServiceClient
216247
GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>();
217248

218-
// Call Microsoft Graph APIs with the agent user identity
249+
// Call Microsoft Graph APIs with the agent user identity using UPN
219250
var me = await graphServiceClient.Me
220251
.GetAsync(r => r.Options.WithAuthenticationOptions(options =>
221252
options.WithAgentUserIdentity(agentIdentity, userUpn)));
253+
254+
// Or using OID
255+
var me = await graphServiceClient.Me
256+
.GetAsync(r => r.Options.WithAuthenticationOptions(options =>
257+
options.WithAgentUserIdentity(agentIdentity, userOid)));
222258
```
223259

224260
### 5. Downstream API Integration
@@ -265,10 +301,15 @@ var response = await downstreamApi.GetForAppAsync<string>(
265301
"MyApi",
266302
options => options.WithAgentIdentity(agentIdentity));
267303

268-
// Call API with agent user identity
304+
// Call API with agent user identity using UPN
269305
var userResponse = await downstreamApi.GetForUserAsync<string>(
270306
"MyApi",
271307
options => options.WithAgentUserIdentity(agentIdentity, userUpn));
308+
309+
// Or using OID
310+
var userResponseByOid = await downstreamApi.GetForUserAsync<string>(
311+
"MyApi",
312+
options => options.WithAgentUserIdentity(agentIdentity, userOid));
272313
```
273314

274315

0 commit comments

Comments
 (0)