WCF 客户端安全性默认为 Windows

本文关键字:Windows 默认 安全性 客户端 WCF | 更新日期: 2023-09-27 18:36:17

我正在尝试使用wsHTTPBinding并使用自定义用户名设置一个简单的WCF Web服务。我在远程服务器设置上具有该服务,并在其他端口上使用自签名证书。例如:https://service.myserice.com:442/service1.svc

出于某种原因,客户端似乎默认为 Windows 安全性,即使我已将其设置为用户名安全性。

这是我的 Web 服务配置文件:

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<customErrors mode="Off"></customErrors>
</system.web>
<system.serviceModel>
<services>
  <service name="AutoSenderWCF.Service1" behaviorConfiguration="Behavior1">
    <host>
      <baseAddresses>
        <add baseAddress="https://service.autosender.com.au:442/" />
      </baseAddresses>
    </host>
    <endpoint address="" binding="wsHttpBinding" bindingName="TransportBinding" contract="AutoSenderWCF.IService1"></endpoint>
    <endpoint address="mex"
      binding="mexHttpBinding"
      contract="IMetadataExchange" />
  </service> 
</services>
<bindings>
  <wsHttpBinding>
    <binding name="TransportBinding">
      <security mode="TransportWithMessageCredential">            
        <message clientCredentialType="UserName" />            
      </security>          
    </binding>        
  </wsHttpBinding>      
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="Behavior1">
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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"/>
      <serviceCredentials>
        <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="AutoSenderWCF.CustomValidator, AutoSenderWCF"/>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

和我的客户端代码(注意安全性=Windows)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="TransportBinding_IService1" 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" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://service.autosender.com.au/Service1.svc"
                binding="wsHttpBinding" bindingConfiguration="TransportBinding_IService1"
                contract="ServiceReference1.IService1" name="TransportBinding_IService1">
                <identity>
                    <servicePrincipalName value="host/wserver" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

WCF 客户端安全性默认为 Windows

明确指定在客户端配置中使用 Windows:

 <security mode="Message">
                        <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>

从服务器配置中复制该部分。

我相信

我已经解决了:这一行。

<endpoint address="" binding="wsHttpBinding" bindingName="TransportBinding" contract="AutoSenderWCF.IService1"></endpoint>

不应该是绑定名称它应该是绑定配置的