Tuesday, February 26, 2008

Client Impersonation in WCF

Recently I got a question about impersonation in WCF. What should we do in order to impersonate the client?

It's simple:

Client Side

// Create a client with given client endpoint configuration
MyServiceClient client = new MyServiceClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;


Server Side



if (ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Impersonation ||   ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Delegation)
{
// Impersonate.
using(ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
{
Console.WriteLine("Impersonating the caller");
}
}

No comments: