docs icon indicating copy to clipboard operation
docs copied to clipboard

IAuthorizationPolicy Evaluate() called after ServiceAuthorizationManager CheckAccessCore(): why?

Open Wallonman opened this issue 7 years ago • 8 comments

The operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets is populated at the time the enumerable is "touched" by mean of a foreach of a GetEnumerator.

This is good to know that the call to the registered IAuthorizationPolicy's is not done prior to entering the CheckAccessCore() method but is delayed to the time the Claimset colleciton is enumerated.

Thank to this post -> https://social.msdn.microsoft.com/Forums/en-US/455bf97a-cc80-4c3b-82cd-82d1a2ef3b0b/iauthorizationpolicy-evaluate-called-after-serviceauthorizationmanager-checkaccesscore-why?forum=netfxnetcom


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Wallonman avatar Jun 21 '19 13:06 Wallonman

Hello. I'm trying to close the loop on these older issues. I'm having a hard time understanding what specific part of the documentation you're referring to and what your suggestion is to fix it. Can you help me understand a bit more?

Thraka avatar Nov 07 '19 20:11 Thraka

@HongGit Perhaps you or your team can handle this?

Thraka avatar Nov 07 '19 20:11 Thraka

@mconnew?

HongGit avatar Nov 07 '19 23:11 HongGit

This issue has been automatically closed due to no response from the original author. Please feel free to reopen it if you have more information that can help us investigate the issue further.

no-response[bot] avatar Nov 21 '19 21:11 no-response[bot]

Sent email to @mconnew

Thraka avatar Dec 02 '19 19:12 Thraka

This issue has been automatically closed due to no response from the original author. Please feel free to reopen it if you have more information that can help us investigate the issue further.

no-response[bot] avatar Dec 02 '19 20:12 no-response[bot]

Sorry for the slow response. As you'll see, this isn't a quick 3 line answer so I needed to find the time to write it up.

Let's start by going over what the purpose of the IAuthorizationPolicy.Evaluate method is. It enables:

  1. Adding more authorization policies
  2. Add properties to the EvaluationContext
  3. Add Claim's to the EvaluationContext

So the reason why you would want the Evaluate method to be called is because you require either of 2 or 3 in your code somewhere (1 just effectively results in more of 2 and 3 happening). The results of these are exposed by the class AuthorizationContext. This means the timing of the call to IAuthorizationPolicy.Evaluate only needs to happen before you need access any properties on AuthorizationContext.

The AuthorizationContext is lazily created so unless you get the AuthorizationContext, you won't cause the Evaluate methods to be called. The timing of calling IAuthorizationPolicy.Evaluate is dependent on when the AuthorizationContext is needed. If you look at the code for ServiceAuthorizationManager.CheckAccessCore here, you can see that the steps are basically:

  1. Call GetAuthorizationPolicies to get collection of IAuthorizationPolicy instances.
  2. Create a new instance of ServiceSecurityContext holding the authorization policies.
  3. Set the ServiceSecurityContext on the property operationContext.IncomingMessageProperties.Security.ServiceSecurityContext

The thing to note is that it's a collection of IAuthorizationPolicy instances that were passed to the constructor of ServiceSecurityContext, not an AuthorizationContext or EvaluationContext. ServiceSecurityContext will lazily create an AuthorizationContext using the passed policies when the AuthorizationContext property is accessed. It's during the creation of the AuthorizationContext that an EvaluationContext is created and the Evaluate method is called on the IAuthorizationPolicy instances. If you don't need to check the AuthorizationContext in the CheckAccessCore method, there's no need to do the extra work of instantiating one. An example scenario where this might be the case is if you are checking for the presence of a specific HTTP header in CheckAccessCore so don't need to access anything on ServiceSecurityContext.

If you don't access ServiceSecurityContext.AuthorizationContext in your code (or any of the properties which use it such as PrimaryIdentity), it's possible the authorization policies are never evaluated. There are some code paths in WCF which will cause the AuthorizationContext to be executed. For example, the method which calls ServiceAuthorizationManager.CheckAccess has code which will accesses ServiceSecurityContext.AuthorizationContext if security auditing is enabled for successful service authorization.

So basically if you need to access the claims from AuthorizationContext, then get them via the property operationContext.IncomingMessageProperties.Security.ServiceSecurityContext.AuthorizationContext.ClaimSets and the authorization policies will be evaluated and will generate any necessary claims.

As the policies are evaluated at the point you first need the output of them being evaluated, it shouldn't matter when they are evaluated as the results will be available when you need them. Because you seem to care about when it happens, it makes me concerned that you might be relying on a side effect of calling the Evaluate method. If that's that case, it's a code smell and you are probably misusing the feature and likely need to achieve what you want a different way. If you don't want to go down that path and just want an easy way to trigger calling Evaluate, just get the ServiceSecurityContext.AuthorizationContext property at the point you expect it to be done (e.g. in the CheckAccessCore method). There's no negative side effects for WCF of triggering this earlier than you need, it's just CPU cycles we're optimistically trying to avoid if not necessary.

mconnew avatar Dec 04 '19 20:12 mconnew

This issue has been closed as part of the issue backlog grooming process outlined in #22351.

That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.

dotnet-bot avatar Jan 25 '21 14:01 dotnet-bot