wcf服務(wù)端配置:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="EndpointBinding">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WcfService.Service1" behaviorConfiguration="httpBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfService/Service1/"/>
</baseAddresses>
</host>
<endpoint address="http://202.196.96.105:8888/Service1" binding="wsHttpBinding" contract="WcfService.IService1" bindingConfiguration="EndpointBinding">
<!--<identity>
<dns value="WCFTest"/>
</identity>-->
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="httpBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
<serviceCredentials>
<!--指定驗(yàn)證方式為Custom,表示自定義,既然是自定義的,就要指出用哪個類進(jìn)行用戶名密碼驗(yàn)證,這里指定了Server程序集中的Server.Validator類,注意這里類完整名稱的寫法-->
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfService.Validator, WcfService" />
<!--指定用于數(shù)據(jù)加密的證書,LocalMachine表示本地計算機(jī),Root表示受信任根證書頒發(fā)機(jī)構(gòu),192.168.90.81是證書標(biāo)題(因?yàn)樽鲎C書時沒指定標(biāo)題,所以使用者默認(rèn)就是標(biāo)題),F(xiàn)indBySubjectName表示按標(biāo)題查找-->
<serviceCertificate storeLocation="LocalMachine" storeName="Root" findValue="WCFTest" x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
?
客戶端代碼:
WSHttpBinding bind = new WSHttpBinding();
EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity("WCFTest");
Uri uri = new Uri("http://202.196.96.2:8888/Service1");
EndpointAddress address = new EndpointAddress(uri, identity);
//EndpointAddress address = new EndpointAddress("http://202.196.96.105:8888/Service1");
ChannelFactory<IService1> cf = new ChannelFactory<IService1>(bind, address);
cf.Credentials.UserName.UserName = "lr";
cf.Credentials.UserName.Password = "liuran123-";
IService1 iservice = cf.CreateChannel();
string str = iservice.GetData("測試成功!");
Console.Write(str);
Console.Read();
服務(wù)端服務(wù)能監(jiān)聽成功,但是在客戶端調(diào)用時會拋出這個異常信息:
Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint
?
希望有遇到這個問題或者知道原因的幫忙看下吧,謝謝了,糾結(jié)了好多天了,就是沒找出問題!感激不盡!
1 回答

HUWWW
TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個贊
原因找到了,我客戶端訪問的時候都用的是代碼加入的配置信息,但是少了message模式下客戶端驗(yàn)證類型:Username(自定義用戶名密碼),所以需要加上一句代碼
bind.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
這樣就ok了,希望對有同樣錯誤的朋友有所幫助。當(dāng)然如果你客戶端訪問是不是用代碼加入的代碼信息,是在配置文件里就不會有這個問題。
- 1 回答
- 0 關(guān)注
- 299 瀏覽
添加回答
舉報
0/150
提交
取消