I'm using MSAL with ADFS2019 and I try to call an internal API on behalf of the authenticated user of my web app. I have a valid access token with some additional claims to identify the user :
{
"aud": "https://SRV",
"iss": "[***REMOVED***]",
"iat": 1687181262,
"nbf": 1687181262,
"exp": 1687184862,
"email": "user1@mydomain.com",
"winaccountname": "user1@mydomain.com",
"anchor": "sub",
"sub": "user1@mydomain.com",
"apptype": "Confidential",
"appid": "https://SRV",
"authmethod": "http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/windows",
"auth_time": "2023-06-19T12:11:56.653Z",
"ver": "1.0",
"scp": "email user_impersonation profile openid"
}
I use that token as UserAssertion when I'm calling the AcquireTokenOnBehalfOf method. The call is successful but there's absolutely no claims in the received token to help me identify the user (only the app) calling the API:
{
"aud": "api://API",
"iss": "[***REMOVED***]",
"iat": 1687181215,
"nbf": 1687181215,
"exp": 1687184815,
"apptype": "Confidential",
"appid": "https://SRV",
"auth_time": "2023-06-19T12:11:56.653Z",
"authmethod": "http://schemas.microsoft.com/ws/2008/06/identity/authenticationmethod/windows",
"ver": "1.0",
"scp": "profile user_impersonation openid email"
}
Here's the code I use to generate the second token :
var token = await HttpContext.GetTokenAsync("access_token");
var resource = _configuration["ADFS:Resource"];
var client = ConfidentialClientApplicationBuilder.Create(_configuration["ADFS:ClientID"]).
WithAdfsAuthority(_configuration["ADFS:Instance"]).
WithClientSecret(_configuration["ADFS:ClientSecret"]).Build();
var userAssertion = new UserAssertion(token, "urn:ietf:params:oauth:grant-type:jwt-bearer");
var result = await client.AcquireTokenOnBehalfOf(new[] { $"{resource}/openid", $"{resource}/email" , $"{resource}/user_impersonation" }, userAssertion)
.WithClaims("sub, upn")
.ExecuteAsync();
var httpClient = _httpClientFactory.CreateClient("WebAPI");
httpClient.BaseAddress = new Uri(_configuration["ServiceUrl"]);
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", result.AccessToken);
Weather = await httpClient.GetFromJsonAsync<IEnumerable<WeatherServiceResponse>>("/WeatherForecast");
return;
What am I missing ? It's probably a stupid mistake but I'm new to all this and there's not a lot of good documentation on how to fix the problem with ADFS (but a lot with AAD).
Thanks.