Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateChannel with ClientCredentials property

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?

like image 584
DA_Prog Avatar asked Mar 03 '26 04:03

DA_Prog


1 Answers

You can set the credentials like this...

Remove default endpoint behavior

ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
var defaultCredentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();
channel.Endpoint.Behaviors.Remove(defaultCredentials); 

Create credentials

ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = "USER";
loginCredentials.UserName.Password = "PASSWORD";

Set the credentials as new endpoint behavior on factory

channel.Endpoint.Behaviors.Add(loginCredentials); 
ICalc proxy = channel.CreateChannel();

EDIT 27 Feb

I suggest adding logging to you service and post the exception you are getting in your Error.svclog file, add below under Configuration tag in your Service app.config.

<system.diagnostics>
        <sources>
            <source name="System.ServiceModel"
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true" >
                <listeners>
                    <add name="xml"/>
                </listeners>
            </source>
            <source name="System.ServiceModel.MessageLogging">
                <listeners>
                    <add name="xml"/>
                </listeners>
            </source>
            <source name="myUserTraceSource"
                    switchValue="Information, ActivityTracing">
                <listeners>
                    <add name="xml"/>
                </listeners>
            </source>
        </sources>
        <sharedListeners>
            <add name="xml"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="Error.svclog" />
        </sharedListeners>
    </system.diagnostics>
like image 165
Milan Raval Avatar answered Mar 05 '26 17:03

Milan Raval



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!