Skip to content

Commit 91b855f

Browse files
authoredJan 5, 2020
MTLS Update (#3962)
* a client cert auth handler * remove workspace.xml * exclude .idea folder when cleaning * ignore workspace xml * add client cert to MTLS sample * add config for client cert authn * add test cert * update client config for new cert * Move cnf generation to extension method * add mtls domain support to discovery * update client to use SocketHandler * rename MTLS middleware * bug in disco * add comments * cleanup mtls middleware * cleanup * cleanup client * add feature to set client cert cnf claim regardless of authentication method * update ignore file * remove rider files * more rider files * use constants for path manipulation * set https fixed
1 parent 939a0ca commit 91b855f

26 files changed

+308
-1512
lines changed
 

‎.gitignore

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
## Ignore Visual Studio temporary files, build results, and
2-
## files generated by popular Visual Studio add-ons.
3-
41
# Rider
5-
.idea/**/workspace.xml
6-
.idea/**/tasks.xml
7-
.idea/**/usage.statistics.xml
8-
.idea/**/dictionaries
9-
.idea/**/shelf
10-
.idea/**/dataSources/
11-
.idea/**/dataSources.ids
12-
.idea/**/dataSources.local.xml
13-
.idea/**/sqlDataSources.xml
14-
.idea/**/dynamic.xml
15-
.idea/**/uiDesigner.xml
16-
.idea/**/dbnavigator.xml
17-
.idea/**/contentModel.xml
2+
.idea
183

194
# User-specific files
205
*.suo
@@ -223,3 +208,4 @@ identityserver4_log.txt
223208
tempkey.rsa
224209
samples/KeyManagement/FileSystem/dataprotectionkeys/
225210
samples/KeyManagement/FileSystem/signingkeys/
211+
workspace.xml

‎clean.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
git clean -xdf -e samples -e src/IdentityServer4/.vs
1+
git clean -xdf -e samples -e src/IdentityServer4/.vs -e .idea
22

33
./clean_cache.sh

‎samples/Clients/.idea/.idea.Clients/.idea/workspace.xml

-323
This file was deleted.

‎samples/Clients/src/ConsoleMTLSClient/ConsoleMTLSClient.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@
99
<ProjectReference Include="..\Constants\Constants.csproj" />
1010
</ItemGroup>
1111

12+
<ItemGroup>
13+
<None Update="client.p12">
14+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15+
</None>
16+
</ItemGroup>
17+
1218
</Project>

‎samples/Clients/src/ConsoleMTLSClient/Program.cs

+22-19
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Linq;
77
using System.Net.Http;
8+
using System.Security.Cryptography.X509Certificates;
89
using System.Threading.Tasks;
910

1011
namespace ConsoleMTLSClient
@@ -24,21 +25,19 @@ public static async Task Main()
2425

2526
static async Task<TokenResponse> RequestTokenAsync()
2627
{
27-
var handler = new HttpClientHandler();
28-
var cert = X509.CurrentUser.My.Thumbprint.Find("bf6e2ca4f07994430b86bf9d48833a33f27a5c24").Single();
29-
handler.ClientCertificates.Add(cert);
28+
var client = new HttpClient(GetHandler());
3029

31-
var client = new HttpClient(handler);
32-
33-
var disco = await client.GetDiscoveryDocumentAsync(Constants.Authority);
30+
var disco = await client.GetDiscoveryDocumentAsync("https://identityserver.local");
3431
if (disco.IsError) throw new Exception(disco.Error);
3532

33+
var endpoint = disco
34+
.TryGetValue(OidcConstants.Discovery.MtlsEndpointAliases)
35+
.Value<string>(OidcConstants.Discovery.TokenEndpoint)
36+
.ToString();
37+
3638
var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
3739
{
38-
Address = disco
39-
.TryGetValue(OidcConstants.Discovery.MtlsEndpointAliases)
40-
.Value<string>(OidcConstants.Discovery.TokenEndpoint)
41-
.ToString(),
40+
Address = endpoint,
4241

4342
ClientId = "mtls",
4443
Scope = "api1"
@@ -50,15 +49,9 @@ static async Task<TokenResponse> RequestTokenAsync()
5049

5150
static async Task CallServiceAsync(string token)
5251
{
53-
var baseAddress = Constants.SampleApi;
54-
55-
var handler = new HttpClientHandler();
56-
var cert = X509.CurrentUser.My.Thumbprint.Find("bf6e2ca4f07994430b86bf9d48833a33f27a5c24").Single();
57-
handler.ClientCertificates.Add(cert);
58-
59-
var client = new HttpClient(handler)
52+
var client = new HttpClient(GetHandler())
6053
{
61-
BaseAddress = new Uri(baseAddress)
54+
BaseAddress = new Uri(Constants.SampleApi)
6255
};
6356

6457
client.SetBearerToken(token);
@@ -67,5 +60,15 @@ static async Task CallServiceAsync(string token)
6760
"\n\nService claims:".ConsoleGreen();
6861
Console.WriteLine(JArray.Parse(response));
6962
}
63+
64+
static SocketsHttpHandler GetHandler()
65+
{
66+
var handler = new SocketsHttpHandler();
67+
68+
var cert = new X509Certificate2("client.p12", "changeit");
69+
handler.SslOptions.ClientCertificates = new X509CertificateCollection { cert };
70+
71+
return handler;
72+
}
7073
}
71-
}
74+
}
3.98 KB
Binary file not shown.

‎src/Directory.Build.targets

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<PackageReference Update="Microsoft.AspNetCore.Identity" Version="$(FrameworkVersion)" />
5353
<PackageReference Update="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="$(FrameworkVersion)" />
5454
<PackageReference Update="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="$(FrameworkVersion)" />
55+
<PackageReference Update="Microsoft.AspNetCore.Authentication.Certificate" Version="$(FrameworkVersion)"/>
5556

5657
<!--microsoft entity framework -->
5758
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkVersion)" />

0 commit comments

Comments
 (0)
Please sign in to comment.