66using  System . Security . Claims ; 
77using  System . Threading . Tasks ; 
88using  Microsoft . AspNetCore . Shared ; 
9+ using  Microsoft . Extensions . DependencyInjection ; 
910using  Microsoft . Extensions . Logging ; 
1011using  Microsoft . Extensions . Options ; 
1112
@@ -17,6 +18,7 @@ namespace Microsoft.AspNetCore.Authorization;
1718public  class  DefaultAuthorizationService  :  IAuthorizationService 
1819{ 
1920    private  readonly  AuthorizationOptions  _options ; 
21+     private  readonly  AuthorizationMetrics ?  _metrics ; 
2022    private  readonly  IAuthorizationHandlerContextFactory  _contextFactory ; 
2123    private  readonly  IAuthorizationHandlerProvider  _handlers ; 
2224    private  readonly  IAuthorizationEvaluator  _evaluator ; 
@@ -32,7 +34,35 @@ public class DefaultAuthorizationService : IAuthorizationService
3234    /// <param name="contextFactory">The <see cref="IAuthorizationHandlerContextFactory"/> used to create the context to handle the authorization.</param> 
3335    /// <param name="evaluator">The <see cref="IAuthorizationEvaluator"/> used to determine if authorization was successful.</param> 
3436    /// <param name="options">The <see cref="AuthorizationOptions"/> used.</param> 
35-     public  DefaultAuthorizationService ( IAuthorizationPolicyProvider  policyProvider ,  IAuthorizationHandlerProvider  handlers ,  ILogger < DefaultAuthorizationService >  logger ,  IAuthorizationHandlerContextFactory  contextFactory ,  IAuthorizationEvaluator  evaluator ,  IOptions < AuthorizationOptions >  options ) 
37+     public  DefaultAuthorizationService ( 
38+         IAuthorizationPolicyProvider  policyProvider , 
39+         IAuthorizationHandlerProvider  handlers , 
40+         ILogger < DefaultAuthorizationService >  logger , 
41+         IAuthorizationHandlerContextFactory  contextFactory , 
42+         IAuthorizationEvaluator  evaluator , 
43+         IOptions < AuthorizationOptions >  options ) 
44+         :  this ( policyProvider ,  handlers ,  logger ,  contextFactory ,  evaluator ,  options ,  services :  null ) 
45+     { 
46+     } 
47+ 
48+     /// <summary> 
49+     /// Creates a new instance of <see cref="DefaultAuthorizationService"/>. 
50+     /// </summary> 
51+     /// <param name="policyProvider">The <see cref="IAuthorizationPolicyProvider"/> used to provide policies.</param> 
52+     /// <param name="handlers">The handlers used to fulfill <see cref="IAuthorizationRequirement"/>s.</param> 
53+     /// <param name="logger">The logger used to log messages, warnings and errors.</param> 
54+     /// <param name="contextFactory">The <see cref="IAuthorizationHandlerContextFactory"/> used to create the context to handle the authorization.</param> 
55+     /// <param name="evaluator">The <see cref="IAuthorizationEvaluator"/> used to determine if authorization was successful.</param> 
56+     /// <param name="options">The <see cref="AuthorizationOptions"/> used.</param> 
57+     /// <param name="services">The <see cref="IServiceProvider"/> used to provide other services.</param> 
58+     public  DefaultAuthorizationService ( 
59+         IAuthorizationPolicyProvider  policyProvider , 
60+         IAuthorizationHandlerProvider  handlers , 
61+         ILogger < DefaultAuthorizationService >  logger , 
62+         IAuthorizationHandlerContextFactory  contextFactory , 
63+         IAuthorizationEvaluator  evaluator , 
64+         IOptions < AuthorizationOptions >  options , 
65+         IServiceProvider ?  services ) 
3666    { 
3767        ArgumentNullThrowHelper . ThrowIfNull ( options ) ; 
3868        ArgumentNullThrowHelper . ThrowIfNull ( policyProvider ) ; 
@@ -47,6 +77,7 @@ public DefaultAuthorizationService(IAuthorizationPolicyProvider policyProvider,
4777        _logger  =  logger ; 
4878        _evaluator  =  evaluator ; 
4979        _contextFactory  =  contextFactory ; 
80+         _metrics  =  services ? . GetService < AuthorizationMetrics > ( ) ; 
5081    } 
5182
5283    /// <summary> 
@@ -59,7 +90,33 @@ public DefaultAuthorizationService(IAuthorizationPolicyProvider policyProvider,
5990    /// A flag indicating whether authorization has succeeded. 
6091    /// This value is <c>true</c> when the user fulfills the policy, otherwise <c>false</c>. 
6192    /// </returns> 
62-     public  virtual  async  Task < AuthorizationResult >  AuthorizeAsync ( ClaimsPrincipal  user ,  object ?  resource ,  IEnumerable < IAuthorizationRequirement >  requirements ) 
93+     public  virtual  Task < AuthorizationResult >  AuthorizeAsync ( ClaimsPrincipal  user ,  object ?  resource ,  IEnumerable < IAuthorizationRequirement >  requirements ) 
94+         =>  AuthorizeCoreAsync ( user ,  resource ,  requirements ,  policyName :  null ) ; 
95+ 
96+     /// <summary> 
97+     /// Checks if a user meets a specific authorization policy. 
98+     /// </summary> 
99+     /// <param name="user">The user to check the policy against.</param> 
100+     /// <param name="resource">The resource the policy should be checked with.</param> 
101+     /// <param name="policyName">The name of the policy to check against a specific context.</param> 
102+     /// <returns> 
103+     /// A flag indicating whether authorization has succeeded. 
104+     /// This value is <c>true</c> when the user fulfills the policy otherwise <c>false</c>. 
105+     /// </returns> 
106+     public  virtual  async  Task < AuthorizationResult >  AuthorizeAsync ( ClaimsPrincipal  user ,  object ?  resource ,  string  policyName ) 
107+     { 
108+         ArgumentNullThrowHelper . ThrowIfNull ( policyName ) ; 
109+ 
110+         var  policy  =  await  _policyProvider . GetPolicyAsync ( policyName ) . ConfigureAwait ( false ) ; 
111+         if  ( policy  ==  null ) 
112+         { 
113+             throw  new  InvalidOperationException ( $ "No policy found: { policyName } .") ; 
114+         } 
115+ 
116+         return  await  AuthorizeCoreAsync ( user ,  resource ,  policy . Requirements ,  policyName ) . ConfigureAwait ( false ) ; 
117+     } 
118+ 
119+     private  async  Task < AuthorizationResult >  AuthorizeCoreAsync ( ClaimsPrincipal  user ,  object ?  resource ,  IEnumerable < IAuthorizationRequirement >  requirements ,  string ?  policyName ) 
63120    { 
64121        ArgumentNullThrowHelper . ThrowIfNull ( requirements ) ; 
65122
@@ -75,6 +132,9 @@ public virtual async Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal us
75132        } 
76133
77134        var  result  =  _evaluator . Evaluate ( authContext ) ; 
135+ 
136+         _metrics ? . AuthorizedRequest ( policyName ,  result ) ; 
137+ 
78138        if  ( result . Succeeded ) 
79139        { 
80140            _logger . UserAuthorizationSucceeded ( ) ; 
@@ -83,28 +143,7 @@ public virtual async Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal us
83143        { 
84144            _logger . UserAuthorizationFailed ( result . Failure ) ; 
85145        } 
86-         return  result ; 
87-     } 
88146
89-     /// <summary> 
90-     /// Checks if a user meets a specific authorization policy. 
91-     /// </summary> 
92-     /// <param name="user">The user to check the policy against.</param> 
93-     /// <param name="resource">The resource the policy should be checked with.</param> 
94-     /// <param name="policyName">The name of the policy to check against a specific context.</param> 
95-     /// <returns> 
96-     /// A flag indicating whether authorization has succeeded. 
97-     /// This value is <c>true</c> when the user fulfills the policy otherwise <c>false</c>. 
98-     /// </returns> 
99-     public  virtual  async  Task < AuthorizationResult >  AuthorizeAsync ( ClaimsPrincipal  user ,  object ?  resource ,  string  policyName ) 
100-     { 
101-         ArgumentNullThrowHelper . ThrowIfNull ( policyName ) ; 
102- 
103-         var  policy  =  await  _policyProvider . GetPolicyAsync ( policyName ) . ConfigureAwait ( false ) ; 
104-         if  ( policy  ==  null ) 
105-         { 
106-             throw  new  InvalidOperationException ( $ "No policy found: { policyName } .") ; 
107-         } 
108-         return  await  this . AuthorizeAsync ( user ,  resource ,  policy ) . ConfigureAwait ( false ) ; 
147+         return  result ; 
109148    } 
110149} 
0 commit comments