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");
}
}
Advertisements

6 Responses to Client Impersonation in WCF

  1. maord says:

    This comment originally written by:Recently I got a question about impersonation in WCF. What should we do in order to impersonate the client? It's simple!

  2. maord says:

    This comment originally written by:JD StuartI'm using Silverlight as a client, and I already enabled my web application to use aspNetContext = true.
    The ServiceSecurityContext.Current is always null.
    How do I impersonate a Active Directory user, in my wcf service, using Silverlight as my client?

  3. maord says:

    This comment originally written by:Maor David-PurHi Marc
    You should catch this issue – probably you cannot impersonate your client.

  4. maord says:

    This comment originally written by:Marc SWhat if ServiceSecurityContext.Current is null?

  5. maord says:

    This comment originally written by:Maor David-PurMr. Lavian, it’s screen resolution issue.
    This is the code:
    if (ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Impersonation ||             ServiceSecurityContext.Current.WindowsIdentity.ImpersonationLevel == TokenImpersonationLevel.Delegation)        
    {   // Impersonate.
       using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())  
     {  Console.WriteLine("Impersonating the caller");  
     }
    }

  6. maord says:

    This comment originally written by:Gilad LavianI cant see the code after:
    "ImpersonationLevel == TokenImpersonationLevel.Impersonation "
    Can you please fix it…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: