Skip to content

Commit bdb67a0

Browse files
Fix Machine Learning Source to Use "clientid" Instead of "client_id" (#5193)
Co-authored-by: Gladwin Johnson <[email protected]>
1 parent 78b686c commit bdb67a0

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

src/client/Microsoft.Identity.Client/Internal/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ internal static class Constants
4242
public const string SshCertAuthHeaderPrefix = "SshCert";
4343

4444
public const string ManagedIdentityClientId = "client_id";
45+
public const string ManagedIdentityClientId2017 = "clientid";
4546
public const string ManagedIdentityObjectId = "object_id";
4647
public const string ManagedIdentityResourceId = "mi_res_id";
4748
public const string ManagedIdentityDefaultClientId = "system_assigned_managed_identity";

src/client/Microsoft.Identity.Client/ManagedIdentity/MachineLearningManagedIdentitySource.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ protected override ManagedIdentityRequest CreateRequest(string resource)
7575
{
7676
case AppConfig.ManagedIdentityIdType.ClientId:
7777
_requestContext.Logger.Info("[Managed Identity] Adding user assigned client id to the request.");
78-
request.QueryParameters[Constants.ManagedIdentityClientId] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
78+
// Use the new 2017 constant for older ML-based environment
79+
request.QueryParameters[Constants.ManagedIdentityClientId2017] = _requestContext.ServiceBundle.Config.ManagedIdentityId.UserAssignedId;
7980
break;
8081

8182
case AppConfig.ManagedIdentityIdType.ResourceId:

tests/Microsoft.Identity.Test.Common/Core/Mocks/MockHttpManagerExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,16 @@ public static void AddManagedIdentityMockHandler(
378378

379379
if (userAssignedIdentityId == UserAssignedIdentityId.ClientId)
380380
{
381-
httpMessageHandler.ExpectedQueryParams.Add(Constants.ManagedIdentityClientId, userAssignedId);
381+
if (managedIdentitySourceType == ManagedIdentitySource.MachineLearning)
382+
{
383+
// For Machine Learning (App Service 2017), the param is "clientid"
384+
httpMessageHandler.ExpectedQueryParams.Add(Constants.ManagedIdentityClientId2017, userAssignedId);
385+
}
386+
else
387+
{
388+
// For App Service 2019, Azure Arc, IMDS, etc., the param is "client_id"
389+
httpMessageHandler.ExpectedQueryParams.Add(Constants.ManagedIdentityClientId, userAssignedId);
390+
}
382391
}
383392

384393
if (userAssignedIdentityId == UserAssignedIdentityId.ResourceId)

tests/Microsoft.Identity.Test.Unit/PublicApiTests/AuthenticationResultTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,31 @@ public async Task MsalTokenResponseParseTestAsync()
190190

191191
}
192192
}
193+
194+
/// <summary>
195+
/// Verifies that if no token type is specified, the default is 'Bearer',
196+
/// and CreateAuthorizationHeader() uses it.
197+
/// </summary>
198+
[TestMethod]
199+
public void DefaultTokenType_IsBearer_Test()
200+
{
201+
DateTime now = DateTime.UtcNow;
202+
203+
var ar = new AuthenticationResult(
204+
accessToken: "some-access-token",
205+
isExtendedLifeTimeToken: false,
206+
uniqueId: "unique-id",
207+
expiresOn: now.AddMinutes(15),
208+
extendedExpiresOn: now.AddMinutes(30),
209+
tenantId: "tid",
210+
account: new Account("aid", "user", "env"),
211+
idToken: "my-id-token",
212+
scopes: new[] { "scope" },
213+
correlationId: Guid.NewGuid()
214+
);
215+
216+
Assert.AreEqual("Bearer", ar.TokenType, "Expected default token type to be 'Bearer'");
217+
Assert.AreEqual("Bearer some-access-token", ar.CreateAuthorizationHeader());
218+
}
193219
}
194220
}

0 commit comments

Comments
 (0)