目录
1 大约思路… 1
2 Nginx集群之WCF布满式身份验证… 1
3 BasicHttpBinding、ws2007HttpBinding. 2
4 Windows证文人成私钥、公钥(X.509证书)… 3
5 编写WCF服务、顾客端程序… 7
6 URL保留项… 13
7 安插WCF服务程序到局域网内3台PC机… 14
8 Nginx集群配置搭建… 15
9 SoapUI和WCF顾客端程序的周转结果… 16
10 总结… 18
1 大致思路
l Nginx集群之WCF遍及式身份验证
l BasicHttpBinding、Ws2007HttpBinding
l Windows证文人成公钥、私钥(x509证书)
l 编写WCF服务、顾客端程序
l URL保留项
l 计划WCF服务程序到局域网内3台PC机
l Nginx集群配置搭建
l SoapUI和WCF顾客端程序的运作结果
l 总结
2 Nginx集群之WCF布满式身份验证
Nginx是个轻量级的反向代理,当然,也是有相应的SSL身份验证。本文首要行使一种本身寄宿的不二法门,使用Nginx集群,通过windows证书(X.509证书),陈诉顾客端怎样访问服务器的方法。
本文源代码首要分类:
l HighlyConcurrentHosting
利用BasicHttpBinding的源代码(本文首要以这种办法开展Nginx集群)
l HighlyConcurrentHosting_Ws2007HttpBinding
行使ws二〇〇五HttpBinding的源代码(那是一种点对点的Windows认证方法)
l Nginx配置
nginx.conf
l Windows证书
wcf_zhyongfeng.cer(顾客端必要设置的注解)
wcf_zhyongfeng.pfx(服务器要求安装的注脚)
以下是本文陈诉的尤为重要组织图:
客户端以BasicHttpBinding实行拜望Nginx,然后Nginx进行负荷均衡,将音信分发到后端自便一台WCF的PC机,后端须要被访问的WCF服务器,都要安装Windows证书(仅被访谈的服务器需求设置wcf_zhyongfegn.pfx证书)。
若以ws二〇〇七HttpBinding进展点对点Windows认证,则Nginx只可以起到通过划分IP绑定特定一台服务器访谈的意义,并不能够起到集群负载均衡,同有时候除了服务器要wcf_zhyongfeng.pfx安装证书外,顾客端也急需张开安装wcf_zhyongfeng.cer证书,这里不作为注重陈诉。
3 BasicHttpBinding、ws2007HttpBinding
那边WCF的Ningx集群,首要用的是BasicHttpBinding。BasicHttpBinding的私下认可安全情势是None,即未有其他安全设置,新闻都是公开传送,对顾客端也不开展求证。
然而basicHttpBinding绑定能够达成景德镇传输,也能够由此传输层和音讯层来担保信息的安全性。
basicHttpBinding设置为Transport安全情势,传输层的黑河是使用IIS的鸡西体制,比方基本身份验证、集成windows验证、SSL安全通道等等
.NET Framework 3.5 介绍了一种用于Web
服务交互称为ws二零零五HttpBinding绑定的新的绑定。这些绑定类似于ws二〇〇七HttpBinding绑定除了它扶助新型的WS-*
音信,安全,可依赖新闻和作业职业。
ws二〇〇七HttpBinding援救的正式:
WS-SecureConversation v1.3 |
WS-Security的扩展,为多个消息交换提供一个安全上下文 |
WS-Trust v1.3 |
WS-Security的扩展,请求并标记问题,管理可依赖关系。 |
WS-SecurityPolicy v1.2 |
WS-Security的安全断言,WS-Security转换以及使用WS-Policy表达的WS-Trust |
Web Services Reliable Messaging v1.1 |
保证消息被传递,适当编码且不会重复接收的协议 |
Web Services Coordination v1.1 |
为分布式平台的动作合作提供协议的平台 |
4 Windows证雅人成私钥、公钥(X.509证书)
进行C:\Windows\system32,以管理人运转cmd.ext
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
C:\Windows\system32>cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin>makecert -r -pe -n "CN=wcf_zhyongfeng" -ss My -sky exchange
Succeeded
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin>
翻看Windows证书的浮动,起始->运维,输入:
certmgr.msc
Windows导出wcf_zhyongfeng.pfx服务端证书:
Windows导出wcf_zhyongfeng.cer客商端证书:
5 编写WCF服务、顾客端程序
l WCF服务程序
Program.cs
using Service;
using System;
using System.ServiceModel;
namespace HighlyConcurrentHosting
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(OutputSomething)))
{
host.Opened += delegate
{
Console.WriteLine(host.Description.Endpoints[0].Address.Uri + "已经启动,按任意键终止服务!");
};
host.Open();
Console.Read();
}
}
}
/// <summary>
/// 证书验证帐户名,密码
/// </summary>
public class UserNamePasswordValidator : System.IdentityModel.Selectors.UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName != "zhyongfeng" || password != "123456")
{
throw new System.IdentityModel.Tokens.SecurityTokenException("Unknown Username or Password");
}
}
}
}
服务端配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<!--指定一个 X.509 证书,用户对认证中的用户名密码加密解密-->
<!--C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin,使用makecert -r -pe -n "CN=wcf_zhyongfeng" -ss My -sky exchange-->
<serviceCertificate findValue="wcf_zhyongfeng" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="My"/>
<clientCertificate>
<!--自定义对客户端进行证书认证方式 这里为 None-->
<authentication certificateValidationMode="None"></authentication>
</clientCertificate>
<!--自定义用户名和密码验证的设置-->
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="HighlyConcurrentHosting.UserNamePasswordValidator,HighlyConcurrentHosting" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<!--这个是需要输入用户名密码的-->
<binding name="YesCertificate">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic"></transport>
<message clientCredentialType="UserName"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="Service.OutputSomething" behaviorConfiguration="metadataBehavior" >
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:5600/hello"/>
</baseAddresses>
</host>
<endpoint binding="basicHttpBinding" bindingConfiguration="YesCertificate" contract="Service.Interface.IOutputSomething"/>
<endpoint binding="basicHttpBinding" bindingConfiguration="YesCertificate" contract="Service.Interface.IOutputSomethingCertificate" />
</service>
</services>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
l 客商端程序
using HighlyConcurrentClient.HighlyConcurrentService;
using System;
using System.Net;
namespace HighlyConcurrentClient
{
class Program
{
static void Main(string[] args)
{
string AddressIP = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
AddressIP = _IPAddress.ToString();
}
}
Console.WriteLine(string.Format("本机IP是:{0}", AddressIP));
using (OutputSomethingCertificateClient proxy = new OutputSomethingCertificateClient())
{
proxy.ClientCredentials.UserName.UserName = "zhyongfeng";
proxy.ClientCredentials.UserName.Password = "123456";
for (int i = 0; i < 20; i++)
{
Console.WriteLine(proxy.GetCertContentData(i));
}
}
Console.Read();
}
}
}
客商端配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IOutputSomething">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
<binding name="BasicHttpBinding_IOutputSomethingCertificate">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://zhyongfeng.com/hello" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IOutputSomething" contract="HighlyConcurrentService.IOutputSomething"
name="BasicHttpBinding_IOutputSomething" />
<endpoint address="http://zhyongfeng.com/hello" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IOutputSomethingCertificate"
contract="HighlyConcurrentService.IOutputSomethingCertificate"
name="BasicHttpBinding_IOutputSomethingCertificate" />
</client>
</system.serviceModel>
</configuration>
顾客端加多引用时,会产生
客户端增多服务引用后,Address恐怕是某一台PC机的IP地址(举例:address=”http://
10.92.202.56:5600/hello”)那是急需修改为以下Nginx的地方
address=”http://zhyongfeng.com/hello“.
即如图所示:
6 URL保留项
详见:http://www.cnblogs.com/yongfeng/p/7851039.html
7 计划WCF服务程序到局域网内3台PC机
长途进行配备WCF服务程序时,需求双击安装服务器wcf_zhyongfeng.pfx证书、修改config三台机的布署文件:10.92.202.56:5600、10.92.202.57:5700、10.92.202.58:5800
然后运维远程Computer的WCF服务程序,运转效果如下:
本机IE上访谈WCF服务端的运营效果:
8 Nginx集群配置搭建
透过自己作主义域名zhyongfeng.com:80端口举办负荷均衡集群访谈,则访谈C:\Windows\System32\drivers\etc\hosts,加多下列“本机IP
自定义的域名”:
10.93.85.66 zhyongfeng.com
针对WCF计划的多台PC机配置(设置了proxy_connect_timeout为10s,假若中间一台机down掉了,能够转账到另一台机械)如下:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream zhyongfeng.com {
server 10.92.202.56:5600;
server 10.92.202.57:5700;
server 10.92.202.58:5800;
}
server {
listen 80;
server_name zhyongfeng.com;
location / {
proxy_pass http://zhyongfeng.com;
proxy_connect_timeout 10s;
}
}
}
运行CMD:
D:\DTLDownLoads\nginx-1.10.2>start nginx
D:\DTLDownLoads\nginx-1.10.2>nginx -s reload
访问WCF服务端:http://zhyongfeng.com/hello,运行结果:
9 SoapUI和WCF顾客端程序的运营结果
Soap公约,可以采用SoapUI测量检验并增加WCF的wsdl:http://zhyongfeng.com/hello?wsdl,运维效果如下:
启航WCF客户端程序,运营作效果果图如下:
远程桌面关掉当中一台10.92.202.56:5600的PC机:
再也开动WCF客户端程序,因为Nginx配置文件设置了proxy_connect_timeout为10s,则关闭的PC机10.92.202.56:5600在10s后会将它的音讯转载给10.92.202.57:5700,继续由另外2台PC机推行:
10 总结
由此运用BasicHttpBinding,除了能让WCF顾客端访谈之外,还扩张了WSDL的会见方式。Nginx集群让WCF顾客端具备顾客名密码验证的同期,到达负载均衡布满式管理的机能。
源代码下载:
http://download.csdn.net/download/ruby_matlab/10126187
PDF下载: