服务器端使用证书来做“自定义用户名密码”认证方式,
服务端代码
<
system.serviceModel
>
< bindings >
< wsHttpBinding >
< binding name = " NewBindingUName " >
< security mode = " Message " >
< transport clientCredentialType = " None " />
< message clientCredentialType = " UserName " />
</ security >
</ binding >
</ wsHttpBinding >
</ bindings >
< services >
< service behaviorConfiguration = " WcfServiceApp.ServicesBehavior "
name = " WcfServiceLib.Services " >
< endpoint address = "" binding = " wsHttpBinding " bindingConfiguration = " NewBindingUName "
contract = " WcfServiceLib.IServices " >
< identity >
< dns value = " 192.168.6.118 " />
</ identity >
</ endpoint >
< endpoint address = " mex " binding = " mexHttpBinding " contract = " IMetadataExchange " />
< host >
< baseAddresses >
< add baseAddress = " http://192.168.6.118/DotNet/OK/Test/WcfLibTest/WcfServiceApp/ " />
</ baseAddresses >
</ host >
</ service >
</ services >
< behaviors >
< serviceBehaviors >
< behavior name = " WcfServiceApp.ServicesBehavior " >
< serviceMetadata httpGetEnabled = " true " />
< serviceDebug includeExceptionDetailInFaults = " false " />
< serviceCredentials >
< serviceCertificate findValue = " WCFServerCert " storeLocation = " LocalMachine "
storeName = " My " x509FindType = " FindBySubjectName " />
< userNameAuthentication userNamePasswordValidationMode = " Custom "
customUserNamePasswordValidatorType = " WcfServiceLib.CustomUserPassword,WcfServiceLib " />
< clientCertificate >
< authentication certificateValidationMode = " None " />
</ clientCertificate >
</ serviceCredentials >
</ behavior >
</ serviceBehaviors >
</ behaviors >
</ system.serviceModel >
< bindings >
< wsHttpBinding >
< binding name = " NewBindingUName " >
< security mode = " Message " >
< transport clientCredentialType = " None " />
< message clientCredentialType = " UserName " />
</ security >
</ binding >
</ wsHttpBinding >
</ bindings >
< services >
< service behaviorConfiguration = " WcfServiceApp.ServicesBehavior "
name = " WcfServiceLib.Services " >
< endpoint address = "" binding = " wsHttpBinding " bindingConfiguration = " NewBindingUName "
contract = " WcfServiceLib.IServices " >
< identity >
< dns value = " 192.168.6.118 " />
</ identity >
</ endpoint >
< endpoint address = " mex " binding = " mexHttpBinding " contract = " IMetadataExchange " />
< host >
< baseAddresses >
< add baseAddress = " http://192.168.6.118/DotNet/OK/Test/WcfLibTest/WcfServiceApp/ " />
</ baseAddresses >
</ host >
</ service >
</ services >
< behaviors >
< serviceBehaviors >
< behavior name = " WcfServiceApp.ServicesBehavior " >
< serviceMetadata httpGetEnabled = " true " />
< serviceDebug includeExceptionDetailInFaults = " false " />
< serviceCredentials >
< serviceCertificate findValue = " WCFServerCert " storeLocation = " LocalMachine "
storeName = " My " x509FindType = " FindBySubjectName " />
< userNameAuthentication userNamePasswordValidationMode = " Custom "
customUserNamePasswordValidatorType = " WcfServiceLib.CustomUserPassword,WcfServiceLib " />
< clientCertificate >
< authentication certificateValidationMode = " None " />
</ clientCertificate >
</ serviceCredentials >
</ behavior >
</ serviceBehaviors >
</ behaviors >
</ system.serviceModel >
客户端调用服务时,提示:
传出消息标识检查失败。所预期的远程终结点的 DNS 标识为“192.168.6.118”,但是远程终结点提供的 DNS 请求为“WCFServerCert”。如果此远程终结点合法,您可以通过在创建通道代理时明确地将 DNS 标识“WCFServerCert”指定为 EndpointAddress 的“标识”属性来解决此问题。
客户端代码
<
system.serviceModel
>
< bindings >
< wsHttpBinding >
< binding name = " WSHttpBinding_IServices " 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://alex/DotNet/OK/Test/WcfLibTest/WcfServiceApp/Services.svc "
binding = " wsHttpBinding " bindingConfiguration = " WSHttpBinding_IServices "
contract = " Services.IServices " name = " WSHttpBinding_IServices " >
< identity >
< dns value = " 192.168.6.118 " />
</ identity >
</ endpoint >
</ client >
</ system.serviceModel >
< bindings >
< wsHttpBinding >
< binding name = " WSHttpBinding_IServices " 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://alex/DotNet/OK/Test/WcfLibTest/WcfServiceApp/Services.svc "
binding = " wsHttpBinding " bindingConfiguration = " WSHttpBinding_IServices "
contract = " Services.IServices " name = " WSHttpBinding_IServices " >
< identity >
< dns value = " 192.168.6.118 " />
</ identity >
</ endpoint >
</ client >
</ system.serviceModel >
如果把“服务端”的
<identity>
<dns value="192.168.6.118" />
</identity>
去掉,就正确了,但总感觉这不是正解,有没有其它的解决办法?