@@ -64,6 +64,62 @@ public async Task AgentUserIdentityGetsTokenForGraphAsync()
6464#endif
6565 }
6666
67+ [ Fact ]
68+ public async Task AgentUserIdentityGetsTokenForGraphWithTenantOverrideAsync ( )
69+ {
70+ string instance = "https://login.microsoftonline.com/" ;
71+ string tenantId = "31a58c3b-ae9c-4448-9e8f-e9e143e800df" ; // Replace with your tenant ID
72+ string agentApplication = "d15884b6-a447-4dd5-a5a5-a668c49f6300" ; // Replace with the actual agent application client ID
73+ string agentIdentity = "d84da24a-2ea2-42b8-b5ab-8637ec208024" ; // Replace with the actual agent identity
74+ string userUpn = "aui1@msidlabtoint.onmicrosoft.com" ; // Replace with the actual user upn.
75+
76+ IServiceCollection services = new ServiceCollection ( ) ;
77+
78+ // Configure the information about the agent application
79+ services . Configure < MicrosoftIdentityApplicationOptions > (
80+ options =>
81+ {
82+ options . Instance = instance ;
83+ options . TenantId = "common" ; // Replace with your tenant ID
84+ options . ClientId = agentApplication ; // Agent application.
85+ options . ClientCredentials = [
86+ CertificateDescription . FromStoreWithDistinguishedName (
87+ "CN=LabAuth.MSIDLab.com" , StoreLocation . LocalMachine , StoreName . My )
88+ ] ;
89+ } ) ;
90+ IServiceProvider serviceProvider = services . ConfigureServicesForAgentIdentitiesTests ( ) ;
91+
92+ // Get an authorization header and handle the call to the downstream API yourself
93+ IAuthorizationHeaderProvider authorizationHeaderProvider = serviceProvider . GetService < IAuthorizationHeaderProvider > ( ) ! ;
94+ AuthorizationHeaderProviderOptions options = new AuthorizationHeaderProviderOptions ( ) . WithAgentUserIdentity (
95+ agentApplicationId : agentIdentity ,
96+ username : userUpn
97+ ) ;
98+ options . AcquireTokenOptions . Tenant = tenantId ;
99+
100+ string authorizationHeaderWithUserToken = await authorizationHeaderProvider . CreateAuthorizationHeaderForUserAsync (
101+ scopes : [ "https://graph.microsoft.com/.default" ] ,
102+ options ) ;
103+ Assert . NotNull ( authorizationHeaderWithUserToken ) ;
104+
105+ // If you want to call Microsoft Graph, just inject and use the Microsoft Graph SDK with the agent identity.
106+ GraphServiceClient graphServiceClient = serviceProvider . GetRequiredService < GraphServiceClient > ( ) ;
107+ var me = await graphServiceClient . Me . GetAsync ( r => r . Options . WithAuthenticationOptions ( options =>
108+ {
109+ options . WithAgentUserIdentity ( agentIdentity , userUpn ) ;
110+ options . AcquireTokenOptions . Tenant = tenantId ;
111+ } ) ) ;
112+ Assert . NotNull ( me ) ;
113+
114+ #if DOWNSTREAM
115+ // If you want to call downstream APIs letting IdWeb handle authentication.
116+ IDownstreamApi downstream = serviceProvider . GetService < IDownstreamApi > ( ) ! ;
117+ string ? response = await downstream . GetForAppAsync < string > ( "api" , options => options . WithAgentIdentity ( "your-agent-identity-here" ) ) ;
118+ response = await downstream . GetForUserAsync < string > ( "api" , options => options . WithAgentIdentity ( "your-agent-identity-here" ) ) ;
119+ #endif
120+ }
121+
122+
67123 [ Fact ]
68124 public async Task AgentUserIdentityGetsTokenForGraphWithCacheAsync ( )
69125 {
0 commit comments