@@ -15,6 +15,7 @@ namespace Microsoft.AspNetCore.Server.IISIntegration
15
15
internal class AuthenticationHandler : IAuthenticationHandler
16
16
{
17
17
private const string MSAspNetCoreWinAuthToken = "MS-ASPNETCORE-WINAUTHTOKEN" ;
18
+ private WindowsPrincipal _user ;
18
19
19
20
internal AuthenticationScheme Scheme { get ; private set ; }
20
21
@@ -33,25 +34,27 @@ public Task<AuthenticateResult> AuthenticateAsync(AuthenticateContext context)
33
34
34
35
private WindowsPrincipal GetUser ( HttpContext httpContext )
35
36
{
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 )
42
38
{
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 ) ;
46
48
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 ) ;
49
51
50
- httpContext . Response . RegisterForDispose ( winIdentity ) ;
51
- winPrincipal = new WindowsPrincipal ( winIdentity ) ;
52
+ httpContext . Response . RegisterForDispose ( winIdentity ) ;
53
+ _user = new WindowsPrincipal ( winIdentity ) ;
54
+ }
52
55
}
53
56
54
- return winPrincipal ;
57
+ return _user ;
55
58
}
56
59
57
60
0 commit comments