使用 BasicHttpBinding 和 Windows 身份验证使用 WCF

Consume WCF with BasicHttpBinding and Windows Authentication

我有一个 WCF 服务,使用 basichttpbinding 和 windows 身份验证(非匿名),

WCF 服务托管在域服务器中 我需要从域外的服务器使用服务,该服务器不加入域,这是一个工作组。那可能吗?请帮忙提供一个代码片段。

配置如下

<bindings>
  <basicHttpBinding>
    <binding name="defaultBinding" receiveTimeout="01:00:00" sendTimeout="01:00:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
      <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647" />
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="defaultServiceBehavior">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <service behaviorConfiguration="defaultServiceBehavior" name="namespace.MyService">
    <endpoint binding="basicHttpBinding" bindingConfiguration="defaultBinding" contract="namespace.IMyService">
      <identity>
        <dns value="abc.xyz.com"/>
      </identity>
    </endpoint>
  </service>
</services>

已编辑:这是我的客户端代码

_securityServiceClient = new SecurityServiceClient(); 
_securityServiceClient.ClientCredentials.UserName.UserName = "lto3";
_securityServiceClient.ClientCredentials.UserName.Password = "Lydowe@1982";

我遇到了这个错误

An exception of type 'System.ServiceModel.Security.MessageSecurityException' occurred in mscorlib.dll but was not handled in user code

Additional information: The HTTP request is unauthorized with client authentication scheme 'Basic'. The authentication header received from the server was 'Negotiate,NTLM'.

尝试使用:

_securityServiceClient.ClientCredentials.Windows.ClientCredential =
            new NetworkCredential("lto3", "Ludowe@1982");

根据 msdn documentation ClientCredentials.Windows 是您在使用 windows 身份验证时指定要使用的凭据的方式。