2

I have a WCF Channel Factory that has its endpoint modified for a WCF service call. What I want is for the service call to be done on a different context which ordinarily is straight forward. However it is not working for me. I can successfully add the credentials to the endpoint behavior, inspect them, and see them but the call is not made using the 'NewUser' credentials.

internal static void UpdateChannelClientBehavior(ChannelFactory factory)
{
   factory.Endpoint.Behaviors.Remove<ClientCredentials>();
   //MyCustomCredentials is a custom class class variable that inherits System.ServiceModel.Description.ClientCredentials
   MyCustomCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
   MyCustomCredentials.Windows.ClientCredential = new NetworkCredential("NewUser", "password", "MyDomain");
   factory.Endpoint.Behaviors.Add(MyCustomCredentials);
   return;
}

So after this code the endpoint indeed has the new Windows Credentials but server side it is still being called under the default context and not with the 'NewUsers' credentials. What am I doing incorrectly to make this work?

Thanks!

2 Answers 2

0

Change your service to utilize the "username" clientCredentialType instead of "Windows". Then change your client side to use the username and password credentials. WCF will check the credentials against the Windows accounts database by default, unless you specify otherwise.

factory.Credentials.UserName.UserName = "NewUser";
factory.Credentials.UserName.Password = "password";

If you use the "Windows" credential type, you cannot change it, it will always be in the context of the running user.

0

Actually this code does work:

internal static void UpdateChannelClientBehavior(ChannelFactory factory)
{
   factory.Endpoint.Behaviors.Remove<ClientCredentials>();
   //MyCustomCredentials is a custom class class variable that inherits System.ServiceModel.Description.ClientCredentials
   MyCustomCredentials.Windows.ClientCredential = new NetworkCredential("NewUser", "password", "MyDomain");
   factory.Endpoint.Behaviors.Add(MyCustomCredentials);
   return;
}

If you were instantiating an instance of the WCF client proxy directly, another way this could be done is as follows:

MyWcfService.MyWcfServiceServiceClient client = new MyWcfService.MyWcfServiceServiceClient();
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("NewUser", "password", "MyDomain");

I was iterating through many attempts of making code changes and has messed up something downstream that was affecting this code. You can indeed change the context of the call using the code above to impersonate a different user making the WCF call. This comes in very handy if needing to temporarily elevate a call to a WCF service.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.