如何在WCF客户端配置文件中指定Windows凭据 [英] How to specify Windows credentials in WCF client configuration file
问题描述
我有一个使用BasicHttpBinding和Windows身份验证的WCF服务。
大多数客户端是域帐户,并使用其默认凭据连接到服务。
I have a WCF service using BasicHttpBinding with Windows authentication. Most clients are domain accounts and connect to the service using their default credentials.
现在,我想从ASP.NET客户端连接到该服务,即在本地帐户下运行。我想使用ASP.NET应用程序可用的Windows凭据(域\用户和密码)连接到WCF服务。
Now I want to connect to the service from an ASP.NET client that is running under a local account. I want to connect to the WCF service using windows credentials (domain\user and password) that are available to the ASP.NET application.
我知道我可以做到这一点在代码中使用ClientBase< T> .ClientCredentials。
I know I can do this in code using ClientBase<T>.ClientCredentials.
是否可以在客户端的web.config文件中指定凭据(域,用户名和密码),所以我
Is there a way to specify the credentials (domain\user and password) in the client's web.config file so I don't have to change the code?
EDIT
是否可以更改代码?不是在配置文件中完成的,有没有一种方法可以使用System.Net.ICredentials或System.Net.NetworkCredential作为WCF服务的凭据?
If it can't be done in the configuration file, is there a way of using System.Net.ICredentials or System.Net.NetworkCredential as a credential for a WCF service?
.NET Framework将它们作为提供网络凭据的同质方式提供,但是对于WCF,似乎已经抛弃了它,而支持基于不相关的System.ServiceModel.Description.ClientCredentials类的新的不兼容系统。
The .NET Framework provides these as a homogenous way to provide network credentials, but with WCF this seems to have been thrown out in favour of a new incompatible system based on the unrelated System.ServiceModel.Description.ClientCredentials class.
编辑2
接受Marc对原始问题的回答-似乎有不能在客户端配置文件中执行此操作:(
Accepting Marc's answer to the original question - it seems there is no way to do this in the client configuration file :(
我认为这是WCF的缺陷-我不接受Microsoft应该故意劝阻我们将凭据放到配置文件中-毕竟它们必须存储在某个位置,并且Framework包含用于加密配置文件的工具。我想我可以为此创建一个自定义的BehaviorExtensionElement,但是应该可以立即使用。
I would see this as a deficiency in WCF - I don't accept that Microsoft should be deliberately discouraging us from putting credentials in the configuration file - after all they have to be stored somewhere, and the Framework includes facilities for encrypting the config file. I guess I could create a custom BehaviorExtensionElement for this, but it ought to be available out of the box.
它也有点不一致:system.net/mailSettings/ smtp /网络配置元素允许指定凭据,那么为什么不使用WCF?
It's also a bit inconsistent: the system.net/mailSettings/smtp/network configuration element allows credentials to be specified, so why not WCF?
关于使用System.Net.NetworkCredential的第二个问题,似乎来自此博客,至少在使用Windows身份验证时,可以使用以下代码:
Regarding the second question about using System.Net.NetworkCredential, it seems from this blog that it is possible, at least when using Windows authentication, with the following code:
factory.Credentials.Windows.ClientCredential =
new System.Net.NetworkCredential(name, password, domain);
推荐答案
您无法在配置文件中指定凭据,不幸的是,您必须在代码中执行此操作(很可能是因为否则,您最终可能会在配置文件中获得纯文本格式的凭据,这不是一件好事...。)。
You can't specify your credentials in the config file, unfortunately - you have to do this in code (most likely because otherwise you might end up with credentials in your config file, in plain text - not a good thing....).
这篇关于如何在WCF客户端配置文件中指定Windows凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!