如何在客户端的配置文件中而不是代码中为 WCF 服务配置用户 ID/密码



>要求是调用Java Web Service。提供了一个 WSDL。呼叫以不安全的方式成功。现在,需要对服务调用进行身份验证。服务调用只能通过狭隘的窗口用户 ID/密码成功。由于我们应用程序中的所有内容都是基于配置的,因此我们不想在代码中对任何内容进行硬编码。如果有人可以展示如何做到这一点,请表示感谢?

顺便说一下,我有这个配置....

  <basicHttpBinding>
        <binding name="MyBinding" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:10:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                     maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" 
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>

  </basicHttpBinding>
不能在

特定于 WCF 的配置中的任何位置设置用户名/密码。但是,您可以将用户名/密码对设置为应用程序设置,从代码中检索它们,然后在 WCF 客户端中设置它们。

<configuration>
  <appSettings>
    <add key="UserName" value="My user name" />
    <add key="Password" value="Your secret password" />
  </appSettings>
</configuration>

并在代码中:

var username = ConfigurationManager.AppSettings["UserName"];
var password = ConfigurationManager.AppSettings["Password"];
client.ClientCredentials.UserName.UserName = username;
client.ClientCredentials.UserName.Password = password;

最新更新