Skip to content

Commit c892032

Browse files
Pass the validate function to the http manager (#5242)
1 parent bbc66fa commit c892032

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public virtual async Task<ManagedIdentityResponse> AuthenticateAsync(
6565
logger: _requestContext.Logger,
6666
doNotThrow: true,
6767
mtlsCertificate: null,
68-
validateServerCertificate: null, cancellationToken: cancellationToken).ConfigureAwait(false);
68+
validateServerCertificate: ValidateServerCertificate,
69+
cancellationToken: cancellationToken).ConfigureAwait(false);
6970
}
7071
else
7172
{
@@ -78,7 +79,8 @@ public virtual async Task<ManagedIdentityResponse> AuthenticateAsync(
7879
logger: _requestContext.Logger,
7980
doNotThrow: true,
8081
mtlsCertificate: null,
81-
validateServerCertificate: null, cancellationToken: cancellationToken)
82+
validateServerCertificate: ValidateServerCertificate,
83+
cancellationToken: cancellationToken)
8284
.ConfigureAwait(false);
8385

8486
}

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ServiceFabricTests.cs

+39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Globalization;
6+
using System.Net.Http;
67
using System.Net.Security;
78
using System.Security.Cryptography.X509Certificates;
89
using System.Threading.Tasks;
@@ -89,5 +90,43 @@ public void ValidateServerCertificateCallback_ServerCertificateValidationCallbac
8990
Assert.IsNotNull(callback);
9091
}
9192
}
93+
94+
[TestMethod]
95+
public async Task SFThrowsWhenGetHttpClientWithValidationIsNotImplementedAsync()
96+
{
97+
using (new EnvVariableContext())
98+
using (var httpManager = new MockHttpManager(isManagedIdentity: true))
99+
{
100+
SetEnvironmentVariables(ManagedIdentitySource.ServiceFabric, "http://localhost:40342/metadata/identity/oauth2/token");
101+
var miBuilder = ManagedIdentityApplicationBuilder.Create(ManagedIdentityId.SystemAssigned)
102+
.WithHttpClientFactory(new MsalSFFactoryNotImplementedException());
103+
104+
// Disabling the shared cache to avoid the test to pass because of the cache
105+
miBuilder.Config.AccessorOptions = null;
106+
var mi = miBuilder.BuildConcrete();
107+
108+
MsalServiceException ex = await Assert.ThrowsExceptionAsync<MsalServiceException>(async () =>
109+
{
110+
await mi.AcquireTokenForManagedIdentity(Resource)
111+
.ExecuteAsync().ConfigureAwait(false);
112+
}).ConfigureAwait(false);
113+
114+
Assert.IsNotNull(ex);
115+
Assert.IsInstanceOfType(ex.InnerException, typeof(NotImplementedException));
116+
}
117+
}
118+
}
119+
120+
internal class MsalSFFactoryNotImplementedException : IMsalSFHttpClientFactory
121+
{
122+
public HttpClient GetHttpClient(Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> validateServerCert)
123+
{
124+
throw new NotImplementedException();
125+
}
126+
127+
public HttpClient GetHttpClient()
128+
{
129+
return new HttpClient();
130+
}
92131
}
93132
}

0 commit comments

Comments
 (0)