tags:

views:

384

answers:

2

Hi there,

I have a website hosted on ServerA which runs using an App Pool using a special user accout with domain privilages to access our database. In the config file of the website I specify:

    <identity impersonate="true" />

I then have a service which is also on ServerA and hosted in a console app programmatically (i.e. no config file) like below.

Uri uri = new Uri("net.tcp://ServerA:9900/Service/");

ServiceHost host = new ServiceHost(typeof(Service1), uri);

NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

ServiceEndpoint serviceEndpoint = host.AddServiceEndpoint(typeof(IService1), binding, uri);
EndpointAddress myEndpointAddress = new EndpointAddress(uri, EndpointIdentity.CreateSpnIdentity("MyspnName"));
serviceEndpoint.Address = myEndpointAddress;

host.Open();

When I open a browser on my local machine and go to the website the website tries to connect to the WCF server and returns the error "The request for security token could not be satisfied because authentication failed."

The website uses the following code to connect to the service:

Uri uri = new Uri("net.tcp://ServerA:9900/Service/");

NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

EndpointIdentity epid = EndpointIdentity.CreateSpnIdentity("MyspnName");
EndpointAddress endPoint = new EndpointAddress(uri, epid);
//EndpointAddress endPoint = new EndpointAddress(uri);

ChannelFactory<IService1> channel = new ChannelFactory<IService1>(binding, endPoint);
channel.Credentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Delegation;
IService1 service = channel.CreateChannel();

service.PrintMessage("Print this message!");

For PrintMessage, the method I'm calling, I tried [OperationBehavior(Impersonation = ImpersonationOption.Required)] and .. .Allowed .. but the error is the same.

When I run the website locally using LocalHost there is no error and it works perfect. And also when I change identity impersonate="false" in my web.config it runs but my windows credentials don't get passed into the WCF service which is the whole point.

Any ideas what I'm missing? Pls no general links, I've probably already read it!

thanks a lot

A: 

If you use Windows authentication, you can grab the identity of the caller in your service code here:

 ServiceSecurityContext.Current.WindowsIdentity

This WindowsIdentity contains things like the ".Name" property, the ".Groups" property of all groups the user belongs to, and more.

If the WindowsIdentity should be NULL, then you don't really have Windows authentication happening.

Are you hosting your WCF service in IIS? Which version - IIS7 is the first one to support net.tcp binding.

What if you self-host your service in a console app - does Windows authentication work then? In that case, it would most likely be a IIS7 config issue of sorts.

Marc

marc_s
The errror happens when i try call the service, so i can't print out any information inside my service. Also you can see above that i am hosting in a console app already
by default, a WCF service won't have anything (like a demo or start page) that you could view in your browser. Can you connect to your service using the WcfTestClient? It's in your Visual Studio "Common7\IDE" directory.
marc_s
Yes they are in the same domain. It works fine with WcfTestClient and also as i explained above it works fine when i run the website localy through VS. It's getting my windows credentials to IIs on ServerA and then on to the WCF service that's the problem
I don't quite understand what you're saying about "give my windows credentials to IIS" - I thought you said you were self-hosting in a console app.......
marc_s
Yes i'm self hosting the WCF service. have a read of the question again. I'm trying to acheieve something similar to this: http://geekswithblogs.net/manesh/archive/2009/04/23/setting-up-wcf-to-impersonate-client-credentials.aspx
But if you're self-hosting - what are you tweaking IIS and its settings for?? If you self-host, there's no IIS in play....
marc_s
A: 

I suspect this is because your service account is not trusted for delegation. It can therefore impersonate the caller for access to local resources, but not for calling out over TCP. Google "Trusted for delegation" for more info.

Joe