任何人都可以指导我如何启用SSL& WCF TCP绑定的证书。任何想法。
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();
感谢
答案 0 :(得分:0)
MessageCredentialType是一个枚举。您可以设置MessageCredentialType.Certificate并设置证书凭据。您应该查看MessageCredentialType enum的文档,您可以在其中找到设置证书凭据的示例。
我用这个例子来验证它是否有效。整个程序看起来像
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();
}
}
}
我的配置文件看起来像这样
<?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>
它对我有用。