我对WCF很陌生,试图用自定义用户名和密码创建WCF服务。我知道我应该将userName和Password设置为代理的ClientCredentials,但由于某种原因,我没有这样的属性...CreateChannel和ClientCredentials属性
我认为它与我的合同有关,所以这里是:
我的合约代码:
namespace Contracts
{
[ServiceContract]
public interface ICalc
{
[OperationContract]
CalcResponse Add(double x, double y);
[OperationContract]
CalcResponse Substract(double x, double y);
[OperationContract]
CalcResponse Multiply(double x, double y);
[OperationContract]
CalcResponse Divide(double x, double y);
}
}
在我的客户都是我尝试做的是:
ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
ICalc proxy = channel.CreateChannel();
proxy.ClientCredentials.UserName.UserName = "USER";
proxy.ClientCredentials.UserName.Password = "PASSWORD";
但我的代理doen't有ClientCredentials财产
更新: 我有一些问题导致一些其他错误。当我解决它们时,我得到一个新的错误:
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.
我的超时设置为5分钟,在客户端和服务器都是5分钟。我得到这个错误后不到一分钟...
这里是我更新的代码:
ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
var defaultCredentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();
channel.Endpoint.Behaviors.Remove(defaultCredentials);
ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = "Comply";
loginCredentials.UserName.Password = "123456";
channel.Endpoint.Behaviors.Add(loginCredentials);
ICalc proxy = channel.CreateChannel();
我有一个自定义的验证,我把一个断点,但它只是没有做它。 .. 我验证码:
class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName == "comply" && password == "123456")
{
}
}
}
的配置(客户端和服务器上):
<service name ="WcfServiceLibrary.Service1" behaviorConfiguration ="CustomValidator">
<host>
<baseAddresses>
<add baseAddress="http://localhost/CalcIISHost/mex"/>
<add baseAddress ="net.tcp://localhost:808/CalcIISHost/CalcService"/>
</baseAddresses>
</host>
<endpoint address ="" binding ="netTcpBinding" bindingConfiguration="tcpWithMessageSecurity" contract ="Contracts.ICalc"></endpoint>
<endpoint address ="net.tcp://localhost:5001/CalcIISHost/mex" binding ="mexTcpBinding" contract ="IMetadataExchange"></endpoint>
</service>
...
<behavior name="CustomValidator">
<serviceCredentials>
<userNameAuthentication
userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="WCFClasses.CustomUserNameValidator, WCFClasses"/>
<serviceCertificate
findValue="localhost"
x509FindType="FindBySubjectName"
storeLocation="CurrentUser"
storeName="My" />
</serviceCredentials>
<serviceMetadata httpGetEnabled ="true"/>
</behavior>
...
<netTcpBinding>
<binding name="tcpWithMessageSecurity" sendTimeout="00:05:00" receiveTimeout="00:05:00">
<security mode="Message" >
<message clientCredentialType="UserName"/>
</security>
</binding>
</netTcpBinding>
任何想法我做错了什么?
谢谢!它似乎工作! :) –
哦,恐怕它没有实际工作...我现在得到一个错误更新的问题... –
我相信错误消息是不相关的传递凭据,我建议你跟踪错误信息。 –