服务配置:
<?xml version="1.0" encoding="utf-8"
?>
<configuration>
<appSettings>
<add key="UID"
value="zcf"/>
<add key="Key" value="123456"/>
</appSettings>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="mySecureBinding">
<security mode="Message">
<transport
clientCredentialType="None"/>
<message
clientCredentialType="UserName"
negotiateServiceCredential="true"
establishSecurityContext="true"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service
behaviorConfiguration="basicBehavior"
name="WcfService.Body.WcfService">
<endpoint
address="WcfService" binding="wsHttpBinding" contract="WcfService.IWcfService"
bindingConfiguration="mySecureBinding">
<identity>
<!--dns
value的值设置为:WMSCert,就是证书的名称-->
<dns value="WMSCert"/>
</identity>
</endpoint>
<endpoint
address="mex" binding="mexHttpBinding" contract="IMetadataExchange"
/>
<host>
<baseAddresses>
<add baseAddress="http://192.168.9.168:8088/Test"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior
name="basicBehavior">
<serviceMetadata
httpGetEnabled="true"/>
<serviceDebug
includeExceptionDetailInFaults="false"/>
<serviceCredentials>
<clientCertificate
>
<authentication
certificateValidationMode="PeerTrust"/><!--受信任证书-->
</clientCertificate>
<serviceCertificate
findValue="WMSCert" x509FindType="FindBySubjectName"
storeLocation="CurrentUser" storeName="TrustedPeople"/>
<!--自定义验证-->
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WineNice.WCF.Validator.UserNamePasswordValidator,WineNice.WCF.Validator"/>
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
服务启动:
ServiceHost sh = new
ServiceHost(typeof(WcfService.Body.WcfService));
sh.Open();
Console.WriteLine("Service is
running!");
foreach
(System.ServiceModel.Description.ServiceEndpoint se in
sh.Description.Endpoints)
{
Console.WriteLine(se.Address.Uri.ToString());
}
Console.ReadLine();
sh.Close();
客户端引用服务
http://192.168.9.168:8088/Test
配置:
<?xml
version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior
name="NewBehavior">
<clientCredentials>
<serviceCertificate>
<!--certificateValidationMode设置为None,测试过PeerTrust,会异常-->
<authentication certificateValidationMode="None"
/>
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IWcfService"
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="UserName"
negotiateServiceCredential="true"
algorithmSuite="Default" establishSecurityContext="true"
/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint
address="http://192.168.9.168:8088/Test/WcfService"
behaviorConfiguration="NewBehavior"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IWcfService"
contract="WcfServiceClient.IWcfService"
name="WSHttpBinding_IWcfService">
<identity>
<dns value="WMSCert" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
客户端调用:
WcfServiceClient.WcfServiceClient wsc = new
WindowsFormsApplication1.WcfServiceClient.WcfServiceClient();
wsc.ClientCredentials.UserName.Password = "12345";
wsc.ClientCredentials.UserName.UserName = "zcf";
MessageBox.Show(wsc.GetData(10));
转载于:https://www.cnblogs.com/GuoQiBuHou/archive/2012/11/30/2795911.html