44using  System . Linq ; 
55using  System . Security . Claims ; 
66using  Microsoft . AspNetCore . Http ; 
7+ using  Microsoft . Extensions . DependencyInjection ; 
78using  Microsoft . Extensions . Options ; 
89
910namespace  Microsoft . AspNetCore . Authentication ; 
@@ -13,6 +14,8 @@ namespace Microsoft.AspNetCore.Authentication;
1314/// </summary> 
1415public  class  AuthenticationService  :  IAuthenticationService 
1516{ 
17+     private  readonly  AuthenticationMetrics ?  _metrics ; 
18+ 
1619    private  HashSet < ClaimsPrincipal > ?  _transformCache ; 
1720
1821    /// <summary> 
@@ -22,12 +25,36 @@ public class AuthenticationService : IAuthenticationService
2225    /// <param name="handlers">The <see cref="IAuthenticationHandlerProvider"/>.</param> 
2326    /// <param name="transform">The <see cref="IClaimsTransformation"/>.</param> 
2427    /// <param name="options">The <see cref="AuthenticationOptions"/>.</param> 
25-     public  AuthenticationService ( IAuthenticationSchemeProvider  schemes ,  IAuthenticationHandlerProvider  handlers ,  IClaimsTransformation  transform ,  IOptions < AuthenticationOptions >  options ) 
28+     public  AuthenticationService ( 
29+         IAuthenticationSchemeProvider  schemes , 
30+         IAuthenticationHandlerProvider  handlers , 
31+         IClaimsTransformation  transform , 
32+         IOptions < AuthenticationOptions >  options ) 
33+         :  this ( schemes ,  handlers ,  transform ,  options ,  services :  null ) 
34+     { 
35+     } 
36+ 
37+     /// <summary> 
38+     /// Constructor. 
39+     /// </summary> 
40+     /// <param name="schemes">The <see cref="IAuthenticationSchemeProvider"/>.</param> 
41+     /// <param name="handlers">The <see cref="IAuthenticationHandlerProvider"/>.</param> 
42+     /// <param name="transform">The <see cref="IClaimsTransformation"/>.</param> 
43+     /// <param name="options">The <see cref="AuthenticationOptions"/>.</param> 
44+     /// <param name="services">The <see cref="IServiceProvider"/>.</param> 
45+     public  AuthenticationService ( 
46+         IAuthenticationSchemeProvider  schemes , 
47+         IAuthenticationHandlerProvider  handlers , 
48+         IClaimsTransformation  transform , 
49+         IOptions < AuthenticationOptions >  options , 
50+         IServiceProvider ?  services ) 
2651    { 
2752        Schemes  =  schemes ; 
2853        Handlers  =  handlers ; 
2954        Transform  =  transform ; 
3055        Options  =  options . Value ; 
56+ 
57+         _metrics  =  services ? . GetService < AuthenticationMetrics > ( ) ; 
3158    } 
3259
3360    /// <summary> 
@@ -77,11 +104,13 @@ public virtual async Task<AuthenticateResult> AuthenticateAsync(HttpContext cont
77104        // Handlers should not return null, but we'll be tolerant of null values for legacy reasons. 
78105        var  result  =  ( await  handler . AuthenticateAsync ( ) )  ??  AuthenticateResult . NoResult ( ) ; 
79106
107+         _metrics ? . AuthenticatedRequest ( scheme ,  result ) ; 
108+ 
80109        if  ( result . Succeeded ) 
81110        { 
82111            var  principal  =  result . Principal ! ; 
83112            var  doTransform  =  true ; 
84-             _transformCache  ??=  new   HashSet < ClaimsPrincipal > ( ) ; 
113+             _transformCache  ??=  [ ] ; 
85114            if  ( _transformCache . Contains ( principal ) ) 
86115            { 
87116                doTransform  =  false ; 
@@ -122,6 +151,8 @@ public virtual async Task ChallengeAsync(HttpContext context, string? scheme, Au
122151            throw  await  CreateMissingHandlerException ( scheme ) ; 
123152        } 
124153
154+         _metrics ? . Challenge ( scheme ) ; 
155+ 
125156        await  handler . ChallengeAsync ( properties ) ; 
126157    } 
127158
@@ -150,6 +181,8 @@ public virtual async Task ForbidAsync(HttpContext context, string? scheme, Authe
150181            throw  await  CreateMissingHandlerException ( scheme ) ; 
151182        } 
152183
184+         _metrics ? . Forbid ( scheme ) ; 
185+ 
153186        await  handler . ForbidAsync ( properties ) ; 
154187    } 
155188
@@ -199,6 +232,8 @@ public virtual async Task SignInAsync(HttpContext context, string? scheme, Claim
199232            throw  await  CreateMismatchedSignInHandlerException ( scheme ,  handler ) ; 
200233        } 
201234
235+         _metrics ? . SignIn ( scheme ) ; 
236+ 
202237        await  signInHandler . SignInAsync ( principal ,  properties ) ; 
203238    } 
204239
@@ -233,6 +268,8 @@ public virtual async Task SignOutAsync(HttpContext context, string? scheme, Auth
233268            throw  await  CreateMismatchedSignOutHandlerException ( scheme ,  handler ) ; 
234269        } 
235270
271+         _metrics ? . SignOut ( scheme ) ; 
272+ 
236273        await  signOutHandler . SignOutAsync ( properties ) ; 
237274    } 
238275
0 commit comments