登录失败WCF错误

本文关键字:错误 WCF 失败 登录 | 更新日期: 2023-09-27 18:16:02

这个场景是:我有两台桌面电脑:

  1. WCF服务托管在ABC桌面机上。
  2. 控制台应用程序创建在CDE桌面。

当我试图用控制台应用程序连接到我的WCF服务时,我得到一个Logon Attempt Failed错误。

WCF Service (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>

Console Application (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>

Console Application (.cs file)

   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(); 
        }

注意:Console Application CS文件中使用的UserName和Password是ABC Desktop机本地的UserName和Password。

登录失败WCF错误

我认为你在web服务上的绑定缺少一些配置:

  <bindings>
<wsHttpBinding>
  <binding name = "someNameForTheBinding">
    <security mode="Message">
      <message clientCredentialType="Windows"/>
    </security>
  </binding>
</wsHttpBinding></bindings>