场景是:我有两台台式机:
- WCF 服务托管在 ABC 桌面计算机上。
- 在 CDE 桌面上创建的控制台应用程序。
当我尝试使用控制台应用程序连接到 WCF 服务时,收到登录尝试失败
错误。
WCF 服务 (web.config
)
<system.serviceModel>
<services>
<service behaviorConfiguration="IISHost.Service1Behavior" name="IISHost.Service1">
<endpoint address="" binding="wsHttpBinding" contract="IISHost.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="IISHost.IISHostBehavior" name="IISHost.IISHost">
<endpoint address="" binding="wsHttpBinding" contract="IISHost.IIISHost">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="IISHost.Service1Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="IISHost.IISHostBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
控制台应用程序 (app.config
)
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IIISHost" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Message">
<transport clientCredentialType="Windows" />
<message clientCredentialType="Windows"
negotiateServiceCredential="true" algorithmSuite="Default"
establishSecurityContext="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://yogeshpc/IISHost/IISHost.svc"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IIISHost"
contract="ServiceReference1.IIISHost"
name="WSHttpBinding_IIISHost">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
控制台应用程序(.cs
文件)
static void Main(string[] args)
{
var d = new IISHostClient();
d.ClientCredentials.Windows.ClientCredential.UserName = "YOGESH";
//d.ClientCredentials.Windows.ClientCredential.Domain = "Workgroup";
d.ClientCredentials.Windows.ClientCredential.Password = "abc@123";
d.ClientCredentials.Windows.AllowNtlm = false;
Console.WriteLine(d.add(30, 30));
Console.ReadLine();
}
注意:控制台应用程序 CS 文件中使用的用户名和密码是 ABC 桌面计算机本地用户名和密码。
最佳答案
我认为您缺少 Web 服务绑定(bind)的一些配置:
<bindings>
<wsHttpBinding>
<binding name = "someNameForTheBinding">
<security mode="Message">
<message clientCredentialType="Windows"/>
</security>
</binding>
</wsHttpBinding></bindings>
您可以查看更多相关信息here.
关于c# - 登录尝试失败 WCF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16841919/