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

Commit c227f53

Browse files
committed
Cache User
1 parent aa98429 commit c227f53

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/Microsoft.AspNetCore.Server.IISIntegration/AuthenticationHandler.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
1515
internal class AuthenticationHandler : IAuthenticationHandler
1616
{
1717
private const string MSAspNetCoreWinAuthToken = "MS-ASPNETCORE-WINAUTHTOKEN";
18+
private WindowsPrincipal _user;
1819

1920
internal AuthenticationScheme Scheme { get; private set; }
2021

@@ -33,25 +34,27 @@ public Task<AuthenticateResult> AuthenticateAsync(AuthenticateContext context)
3334

3435
private WindowsPrincipal GetUser(HttpContext httpContext)
3536
{
36-
var tokenHeader = httpContext.Request.Headers[MSAspNetCoreWinAuthToken];
37-
38-
int hexHandle;
39-
WindowsPrincipal winPrincipal = null;
40-
if (!StringValues.IsNullOrEmpty(tokenHeader)
41-
&& int.TryParse(tokenHeader, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hexHandle))
37+
if (_user == null)
4238
{
43-
// Always create the identity if the handle exists, we need to dispose it so it does not leak.
44-
var handle = new IntPtr(hexHandle);
45-
var winIdentity = new WindowsIdentity(handle);
39+
var tokenHeader = httpContext.Request.Headers[MSAspNetCoreWinAuthToken];
40+
41+
int hexHandle;
42+
if (!StringValues.IsNullOrEmpty(tokenHeader)
43+
&& int.TryParse(tokenHeader, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out hexHandle))
44+
{
45+
// Always create the identity if the handle exists, we need to dispose it so it does not leak.
46+
var handle = new IntPtr(hexHandle);
47+
var winIdentity = new WindowsIdentity(handle);
4648

47-
// WindowsIdentity just duplicated the handle so we need to close the original.
48-
NativeMethods.CloseHandle(handle);
49+
// WindowsIdentity just duplicated the handle so we need to close the original.
50+
NativeMethods.CloseHandle(handle);
4951

50-
httpContext.Response.RegisterForDispose(winIdentity);
51-
winPrincipal = new WindowsPrincipal(winIdentity);
52+
httpContext.Response.RegisterForDispose(winIdentity);
53+
_user = new WindowsPrincipal(winIdentity);
54+
}
5255
}
5356

54-
return winPrincipal;
57+
return _user;
5558
}
5659

5760

src/Microsoft.AspNetCore.Server.IISIntegration/IISMiddleware.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7-
using System.Globalization;
87
using System.Linq;
9-
using System.Security.Principal;
108
using System.Threading.Tasks;
119
using Microsoft.AspNetCore.Authentication;
1210
using Microsoft.AspNetCore.Builder;
1311
using Microsoft.AspNetCore.Http;
1412
using Microsoft.AspNetCore.Http.Features;
15-
using Microsoft.Extensions.Internal;
1613
using Microsoft.Extensions.Logging;
1714
using Microsoft.Extensions.Options;
1815
using Microsoft.Extensions.Primitives;
@@ -59,8 +56,8 @@ public IISMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IOption
5956
throw new InvalidOperationException("IServiceCollection.AddAuthentication() is required to use Authentication.");
6057
}
6158

62-
auth.AddScheme(new AuthenticationScheme("NTLM", typeof(AuthenticationHandler), new Dictionary<string, object>()));
63-
auth.AddScheme(new AuthenticationScheme("Negotiate", typeof(AuthenticationHandler), new Dictionary<string, object>()));
59+
auth.AddScheme(new AuthenticationScheme("NTLM", typeof(AuthenticationHandler)));
60+
auth.AddScheme(new AuthenticationScheme("Negotiate", typeof(AuthenticationHandler)));
6461
}
6562

6663
_pairingToken = pairingToken;

0 commit comments

Comments
 (0)