Skip to content
This repository was archived by the owner on Nov 1, 2018. It is now read-only.

Commit 4b77942

Browse files
committed
#27 Forward client certificates.
Conflicts: src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerMiddleware.cs
1 parent b2a3f87 commit 4b77942

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

samples/IISSample/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerfactory)
2626
await context.Response.WriteAsync("User - " + context.User.Identity.Name + Environment.NewLine);
2727
await context.Response.WriteAsync("PathBase: " + context.Request.PathBase.Value + Environment.NewLine);
2828
await context.Response.WriteAsync("Path: " + context.Request.Path.Value + Environment.NewLine);
29+
await context.Response.WriteAsync("ClientCert: " + context.Connection.ClientCertificate + Environment.NewLine);
30+
31+
await context.Response.WriteAsync(Environment.NewLine + "Headers:" + Environment.NewLine);
2932
foreach (var header in context.Request.Headers)
3033
{
3134
await context.Response.WriteAsync(header.Key + ": " + header.Value + Environment.NewLine);
3235
}
36+
37+
await context.Response.WriteAsync(Environment.NewLine + "Environment Variables:" + Environment.NewLine);
3338
var vars = Environment.GetEnvironmentVariables();
3439
foreach (var key in vars.Keys)
3540
{
3641
var value = vars[key];
3742
await context.Response.WriteAsync(key + ": " + value + Environment.NewLine);
3843
}
44+
45+
// throw new Exception("Test Exception");
3946
});
4047
}
4148

src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerMiddleware.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
using System;
55
using System.Globalization;
6+
using System.Security.Cryptography.X509Certificates;
67
using System.Security.Principal;
78
using System.Threading.Tasks;
89
using Microsoft.AspNet.Builder;
910
using Microsoft.AspNet.Http;
1011
using Microsoft.AspNet.Http.Features.Authentication;
1112
using Microsoft.AspNet.Http.Features.Authentication.Internal;
1213
using Microsoft.Extensions.Internal;
14+
using Microsoft.Extensions.Logging;
1315
using Microsoft.Extensions.Options;
1416
using Microsoft.Extensions.Primitives;
1517

@@ -18,27 +20,52 @@ namespace Microsoft.AspNet.IISPlatformHandler
1820
public class IISPlatformHandlerMiddleware
1921
{
2022
private const string XIISWindowsAuthToken = "X-IIS-WindowsAuthToken";
23+
private const string MSPlatformHandlerClientCert = "MS-PLATFORM-HANDLER-CLIENTCERT";
2124

2225
private readonly RequestDelegate _next;
2326
private readonly IISPlatformHandlerOptions _options;
27+
private readonly ILogger _logger;
2428

25-
public IISPlatformHandlerMiddleware(RequestDelegate next, IOptions<IISPlatformHandlerOptions> options)
29+
public IISPlatformHandlerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOptions<IISPlatformHandlerOptions> options)
2630
{
2731
if (next == null)
2832
{
2933
throw new ArgumentNullException(nameof(next));
3034
}
35+
if (loggerFactory == null)
36+
{
37+
throw new ArgumentNullException(nameof(loggerFactory));
38+
}
3139
if (options == null)
3240
{
3341
throw new ArgumentNullException(nameof(options));
3442
}
3543

3644
_next = next;
3745
_options = options.Value;
46+
_logger = loggerFactory.CreateLogger<IISPlatformHandlerMiddleware>();
3847
}
3948

4049
public async Task Invoke(HttpContext httpContext)
4150
{
51+
if (_options.FlowClientCertificate)
52+
{
53+
var header = httpContext.Request.Headers[MSPlatformHandlerClientCert];
54+
if (!StringValues.IsNullOrEmpty(header))
55+
{
56+
try
57+
{
58+
var bytes = Convert.FromBase64String(header);
59+
var cert = new X509Certificate2(bytes);
60+
httpContext.Connection.ClientCertificate = cert;
61+
}
62+
catch (Exception ex)
63+
{
64+
_logger.LogWarning("Failed to apply the client certificate.", ex);
65+
}
66+
}
67+
}
68+
4269
if (_options.FlowWindowsAuthentication)
4370
{
4471
var winPrincipal = UpdateUser(httpContext);

src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public class IISPlatformHandlerOptions
2222
/// </summary>
2323
public bool FlowWindowsAuthentication { get; set; } = true;
2424

25+
/// <summary>
26+
/// Populates the ITLSConnectionFeature if the MS-PLATFORM-HANDLER-CLIENTCERT request header is present.
27+
/// </summary>
28+
public bool FlowClientCertificate { get; set; } = true;
29+
2530
/// <summary>
2631
/// Additional information about the authentication type which is made available to the application.
2732
/// </summary>

src/Microsoft.AspNet.IISPlatformHandler/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*",
1414
"Microsoft.AspNet.Http": "1.0.0-*",
1515
"Microsoft.AspNet.Http.Extensions": "1.0.0-*",
16+
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-*",
1617
"Microsoft.Extensions.Options": "1.0.0-*",
1718
"Microsoft.Extensions.SecurityHelper.Sources": {
1819
"type": "build",

0 commit comments

Comments
 (0)