Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/Microsoft.IdentityModel.Protocols/HttpRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class HttpRequestData
{
private IDictionary<string, IEnumerable<string>> _headers = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);
private X509Certificate2Collection _clientCertificates;
private Action<X509Certificate2Collection> _lazyClientCertificates;
private readonly object _lazyClientCertificatesLock = new object();

/// <summary>
/// Gets or sets the http request URI.
Expand Down Expand Up @@ -51,9 +53,29 @@ public IDictionary<string, IEnumerable<string>> Headers
/// <summary>
/// Gets the certificate collection involved in authenticating the client against the server.
/// </summary>
public X509Certificate2Collection ClientCertificates => _clientCertificates ??
Interlocked.CompareExchange(ref _clientCertificates, [], null) ??
_clientCertificates;
public X509Certificate2Collection ClientCertificates
{
get
{
_clientCertificates ??=
Interlocked.CompareExchange(ref _clientCertificates, [], null) ??
_clientCertificates;

if (_lazyClientCertificates != null)
{
lock (_lazyClientCertificatesLock)
{
if (_lazyClientCertificates != null)
{
_lazyClientCertificates(_clientCertificates);
_lazyClientCertificates = null;
}
}
}

return _clientCertificates;
}
}

/// <summary>
/// Gets or sets an <see cref="IDictionary{String, Object}"/> that enables custom extensibility scenarios.
Expand All @@ -77,5 +99,17 @@ public void AppendHeaders(HttpHeaders headers)
Headers.Add(header.Key, header.Value);
}
}

/// <summary>
/// Sets an action to lazily populate the <see cref="ClientCertificates"/> property.
/// </summary>
/// <param name="lazyClientCertificates">The action to lazily populate the property.</param>
public void SetLazyClientCertificates(Action<X509Certificate2Collection> lazyClientCertificates)
{
if (lazyClientCertificates == null)
return;

_lazyClientCertificates = lazyClientCertificates;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.IdentityModel.Protocols.HttpRequestData.SetLazyClientCertificates(System.Action<System.Security.Cryptography.X509Certificates.X509Certificate2Collection> lazyClientCertificates) -> void
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,33 @@ public void ClientCertificates()
Assert.Single(httpRequestData.ClientCertificates);
Assert.Equal(cert, httpRequestData.ClientCertificates[0]);
}

[Fact]
public void LazyClientCertificates()
{
var httpRequestData = new HttpRequestData();
Assert.NotNull(httpRequestData.ClientCertificates);
Assert.Empty(httpRequestData.ClientCertificates);

X509Certificate2 cert = TestUtils.CertificateHelper.LoadX509Certificate(KeyingMaterial.AADCertData);

int numberCertsPopulatedCalled = 0;
httpRequestData.SetLazyClientCertificates((c) =>
{
numberCertsPopulatedCalled++;
c.Add(cert);
});

Assert.Equal(0, numberCertsPopulatedCalled);

Assert.Single(httpRequestData.ClientCertificates);
Assert.Equal(cert, httpRequestData.ClientCertificates[0]);
Assert.Equal(1, numberCertsPopulatedCalled);

// Invoke again, should not call the delegate again
_ = httpRequestData.ClientCertificates;

Assert.Equal(1, numberCertsPopulatedCalled);
}
}
}