User Impersonation in WCF
In some integration scenarios, impersonation is required, where a WCF service is required to assume the caller’s identity. Though this usually happens for a single call, the impersonation token could be retained for future use by the WCF servce. The reason impersonation is most relevant to integration scenarios is because all WCF service code running under the priviledges of the caller ensures that only resources and data available to the caller are made available.
I found this to be partcularly useful while integrating middle layers of various heterogeneous systems. I did not have to add additional integration code to ensure that application-level authorization was being implemented because I could be assured that the impersonated thread would be denied access if relevant permissions to resources have not been authorized in the target system.
Inline code to support impersonation in WCF is as shown below:
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public int AddNumbers(int i, int j)
{
return (i + j);
}
To implement impersonation on all Operations, a behavior could be configured as shown below:
<behaviors>
<serviceBehaviors>
<behavior name=“ServiceBehavior“>
<serviceAuthorization principalPermissionMode=“UseWindowsGroups“
impersonateCallerForAllOperations=“true“>
</serviceAuthorization>
</behavior>
</serviceBehaviors>
</behaviors>


