Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add client certificates collection to HttpRequestData #2462

Merged
merged 1 commit into from
Jan 29, 2024
Merged
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
11 changes: 11 additions & 0 deletions src/Microsoft.IdentityModel.Protocols/HttpRequestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace Microsoft.IdentityModel.Protocols
{
Expand All @@ -14,6 +16,7 @@ namespace Microsoft.IdentityModel.Protocols
public class HttpRequestData
{
private IDictionary<string, IEnumerable<string>> _headers = new Dictionary<string, IEnumerable<string>>(StringComparer.OrdinalIgnoreCase);
private X509Certificate2Collection _clientCertificates;

/// <summary>
/// Gets or sets the http request URI.
Expand Down Expand Up @@ -44,6 +47,14 @@ public IDictionary<string, IEnumerable<string>> Headers
_headers = value ?? throw new ArgumentNullException(nameof(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;

/// <summary>
/// Gets or sets an <see cref="IDictionary{String, Object}"/> that enables custom extensibility scenarios.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Security.Cryptography.X509Certificates;
using Microsoft.IdentityModel.TestUtils;
using Xunit;

namespace Microsoft.IdentityModel.Protocols.Tests
{
public class HttpRequestDataTests
{
[Fact]
public void ClientCertificates()
{
var httpRequestData = new HttpRequestData();
Assert.NotNull(httpRequestData.ClientCertificates);
Assert.Empty(httpRequestData.ClientCertificates);

var cert = new X509Certificate2(Convert.FromBase64String(KeyingMaterial.AADCertData));
httpRequestData.ClientCertificates.Add(cert);

Assert.Single(httpRequestData.ClientCertificates);
Assert.Equal(cert, httpRequestData.ClientCertificates[0]);
}
}
}
Loading