Impersonating with Windows Security
I have a service that uses Windows authentication and want to impersonate the caller in one of the service operations. How should I configure the client and service?
There are a couple of things you need to do here to make this work.
On the client side, you need to give the client proxy your Windows credentials and permit it to perform the impersonation. The exact level of impersonation you need is something determined by your application. I'm using plain old Impersonate level impersonation in this example.
client.ClientCredentials.Windows.ClientCredential.Domain = "MYDOMAIN";
client.ClientCredentials.Windows.ClientCredential.UserName = "User";
client.ClientCredentials.Windows.ClientCredential.Password = "Password";
client.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
Your service process needs the SeImpersonatePrivilege.
Your operation needs to perform the impersonation. If you just want to impersonate for part of the operation, you can scope the impersonation block.
using (ServiceSecurityContext.Current.WindowsIdentity.Impersonate())
{
...
}
You'd probably want to have some error handling for that block. If you want to impersonate for all of the operation, you can add an attribute to modify the operation behavior.
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
Finally, you can add a service behavior to instruct the service to impersonate the caller for all operations when allowed.
host.Description.Behaviors.Find<ServiceAuthorizationBehavior>().ImpersonateCallerForAllOperations = true;
Next time: Use OneWay for Long-Running Operations
Comments
Anonymous
October 12, 2006
I’m trying to use a Certificate credential with security mode TransportWithMessageCredential. CertificateAnonymous
October 21, 2006
This is probably the coolest blog I've come across in a while. :) http://blogs.msdn.com/drnick/archive/2006/10/12/impersonating-with-windows-security.aspxtx,StevAnonymous
April 17, 2008
How can I run a service operation hosted in IIS using a specific identity? There are two ways for your