c# - 登录尝试失败 WCF 错误

标签 c# asp.net .net wcf wcf-binding

场景是:我有两台台式机:

  1. WCF 服务托管在 ABC 桌面计算机上。
  2. 在 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/

相关文章:

c# - 如何在 C# 中将正则表达式设置为 MaskedTextBox 的掩码?

c# - 正则表达式来分割数字和运算符

asp.net - 使用 jQuery 和参数在 MVC 中调用操作方法不起作用

asp.net - 我可以合并 SignalR 和 RESTful API 吗?

c# - 使用 jquery 超时 Ajax 请求?

c# - 多个文本框 WPF 的一个功能

c# - 如何使用 Storyboard 在 Grid 上应用 RenderTransform.TranslateTransform?

c# - 避免将“转换为”

c# - 有没有更好的方法来使用依赖注入(inject)来实例化接口(interface)?

.net - 正则表达式匹配