简体   繁体   English

WCF并传递Windows凭据

[英]WCF and passing windows credentials

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. 我有一个托管在ServerA上的网站,该网站使用应用程序池运行,使用具有域权限的特殊用户帐户来访问我们的数据库。 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 (ie no config file) like below. 然后,我有一个也在ServerA上的服务,并以编程方式托管在控制台应用程序中(即没有配置文件),如下所示。

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." 当我在本地计算机上打开浏览器并转到网站时,网站会尝试连接到WCF服务器并返回错误“由于身份验证失败,无法满足安全令牌请求”。

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. 对于PrintMessage,我正在调用的方法,我尝试[OperationBehavior(Impersonation = ImpersonationOption.Required)]和..。允许..但错误是相同的。

When I run the website locally using LocalHost there is no error and it works perfect. 当我使用LocalHost本地运行网站时,没有错误,它完美无缺。 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. 而且当我在我的web.config中更改身份impersonate =“false”时,它会运行,但我的Windows凭据不会被传递到WCF服务,这就是整点。

Any ideas what I'm missing? 我缺少什么想法? Pls no general links, I've probably already read it! 请不要一般链接,我可能已经读过了!

thanks a lot 非常感谢

If you use Windows authentication, you can grab the identity of the caller in your service code here: 如果您使用Windows身份验证,则可以在此处获取服务代码中呼叫者的身份:

 ServiceSecurityContext.Current.WindowsIdentity

This WindowsIdentity contains things like the ".Name" property, the ".Groups" property of all groups the user belongs to, and more. 此WindowsIdentity包含诸如“.Name”属性,用户所属的所有组的“.Groups”属性等内容。

If the WindowsIdentity should be NULL, then you don't really have Windows authentication happening. 如果WindowsIdentity应为NULL,那么您实际上并没有进行Windows身份验证。

Are you hosting your WCF service in IIS? 您是否在IIS中托管WCF服务? Which version - IIS7 is the first one to support net.tcp binding. 哪个版本 - IIS7是第一个支持net.tcp绑定的版本。

What if you self-host your service in a console app - does Windows authentication work then? 如果您在控制台应用程序中自托管服务怎么办?Windows身份验证是否有效? In that case, it would most likely be a IIS7 config issue of sorts. 在这种情况下,它很可能是一个IIS7配置问题。

Marc

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. 因此,它可以模拟调用者访问本地资源,但不能通过TCP调用。 Google "Trusted for delegation" for more info. 谷歌“信任代表团”了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM