Question

any one can guide me how to enable SSL & Certificate in case WCF TCP binding. any idea.

var baseAddress = "localhost";
var factory = new DuplexChannelFactory<IMyWCFService>(new InstanceContext(SiteServer.Instance));
factory.Endpoint.Address = new EndpointAddress("net.tcp://{0}:8000/".Fmt(baseAddress));
var binding = new NetTcpBinding(SecurityMode.Message);
binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
factory.Endpoint.Binding = binding;
var u = factory.Credentials.UserName;
u.UserName = userName;
u.Password = password;
return factory.CreateChannel();

thanks

Était-ce utile?

La solution

MessageCredentialType is an enum. You can set MessageCredentialType.Certificate and set certificate credentials. You should look at documentation of MessageCredentialType enum where you can find example of setting certificate credentials.

I used this example to verify that it works. whole program looked like

using System;
using System.ServiceModel;

namespace ConsoleApplication2
{
    [ServiceContract(Namespace = "http://UE.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract(IsOneWay = false)]
        double Add(double n1, double n2);

        [OperationContract(IsOneWay = false)]
        double Subtract(double n1, double n2);

        [OperationContract(IsOneWay = false)]
        double Multiply(double n1, double n2);

        [OperationContract(IsOneWay = false)]
        double Divide(double n1, double n2);
    }

    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }

        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
    }


    public class Client : ClientBase<ICalculator>, ICalculator
    {

        public double Add(double n1, double n2)
        {
            return base.Channel.Add(n1, n2);
        }

        public double Subtract(double n1, double n2)
        {
            throw new NotImplementedException();
        }

        public double Multiply(double n1, double n2)
        {
            throw new NotImplementedException();
        }

        public double Divide(double n1, double n2)
        {
            throw new NotImplementedException();
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            ServiceHost myServiceHost = new ServiceHost(typeof(CalculatorService));

            // Open the ServiceHostBase to create listeners and start listening for messages.
            myServiceHost.Open();

            // The service can now be accessed.
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();

            Client c = new Client();
            var res = c.Add(1, 2);

            Console.ReadLine();
        }

    }
}

My configuration file looked like this

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <client>
      <endpoint address="net.tcp://localhost:8000/servicemodelsamples/service/calc" binding="netTcpBinding" contract="ConsoleApplication2.ICalculator" behaviorConfiguration="net" >
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
    </client>
    <services>
      <service name="ConsoleApplication2.CalculatorService" behaviorConfiguration="service">
        <endpoint address="net.tcp://localhost:8000/servicemodelsamples/service/calc" binding="netTcpBinding" contract="ConsoleApplication2.ICalculator" >
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8000/servicemodelsamples/service" />
          </baseAddresses>
        </host>
      </service>

    </services>
    <bindings>
      <netTcpBinding>
        <binding>
          <security mode="Message">
            <message clientCredentialType="Certificate"/>
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="service">
          <serviceCredentials>
            <serviceCertificate findValue="localhost" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="net">
          <clientCredentials>
            <clientCertificate findValue="localhost" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectName" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

It worked for me.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top