I'm pretty new to WCF and trying to create a WCF service with custom username and password. I know that I should set the userName and Password to the proxy's ClientCredentials, but out of some reason, I have no such a property...
I assume it has something to do with my Contract, so here it is:
My contract code:
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);
}
}
In my client all I try to do is:
ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
ICalc proxy = channel.CreateChannel();
proxy.ClientCredentials.UserName.UserName = "USER";
proxy.ClientCredentials.UserName.Password = "PASSWORD";
But my proxy doen't have a ClientCredentials property
Update: I had some issues that causes some other errors. As I solved them, I got a new error:
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.
My timeout is 5 set to 5 minutes in both client and server. I get this error after less than a minute...
Here's my updated code:
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();
I have a custom validator, I put a breakpoint there, but it just didn't make it there... My validator code:
class CustomUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName == "comply" && password == "123456")
{
}
}
}
The config (on both client and server):
<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>
Any ideas what have i done wrong?