WCF安全系列(三) - netTCPBinding绑定之Message安全模式

一、         netTCPBinding. 1

3           安全模式 – Message. 1

3.1.         客户端验证 – None. 2

3.1.1.     获得和安装证书... 2

3.1.2.     服务端代码... 2

3.1.3.     客户端代码... 3

3.1.4.     测试... 3

3.2.         客户端验证 – Windows. 3

3.2.1.     获得和安装证书... 4

3.2.2.     服务端代码... 4

3.2.3.     客户端代码... 4

3.2.4.     测试... 5

3.3.         客户端验证 – UserName. 5

3.3.1.     获得和安装证书... 5

3.3.2.     服务端代码... 6

3.3.3.     客户端代码... 8

3.3.4.     测试... 9

3.3.5.     身份模拟和访问权限控制... 9

3.4.         客户端验证:Certificate. 10

3.4.1.     获得和安装证书... 10

3.4.2.     服务端代码... 11

3.4.3.     客户端代码... 11

3.4.4.     测试... 12

3.4.5.     证书映射到windows用户... 12

 

一、  netTCPBinding

此绑定使用TCP传输协议,不具交互性,只适用于 WCF WCF 的通信。

此绑定的传输安全性的实现:

l  安全模式Message

这种模式WCF中都一样,都是使用WS-*通过对SOAP消息本身进行加密、签名等等的处理来保证安全性。Message模式不依赖于传输协议。服务端需要指定服务端证书,用来加密服务端和客户端相互传送的消息。

l  Transport – 客户端windows验证

使用windows security保证消息的安全,使用windows credential进行身份验证。

这种方式不需要服务端证书。

至于windows security的实现安全的原理我还不明白,这部分尚待了解。

l  Transport – 客户端其他验证方式

使用TLS over TCP实现传输安全性,需要服务端证书。

一般大家对SSL比较熟悉,对TLS可能要陌生些,其实可以说TLS协议可以看作跟SSL协议后续版本。1994年,netscape为了在internet上进行安全的数据传输,开发了的SSL协议,后来标准化组织把SSL标准化了,稍作修改改名叫TLS,在一般的使用意义上,这两个协议差别不大,就是在保证消息完整性的散列算法上使用了不同的算法。

TLS over TCP 直接建立在TCP协议上,通过传输层TCP协议实现安全性。

 

netTCPBinding绑定是直接使用TCP协议,不走HTTP,所以不能使用IIS宿主。这部分的测试实例采用自宿主的服务端console应用,基于代码的方式。

 

3、 安全模式 – Message

这部分测试netTCPBinding绑定的Message安全模式的各种情况。

共用测试WCF服务类

所有测试都是用同样的服务端contract和实现这个contractservice

[ServiceContract(Namespace = "http://chnking.com")]

public interface IGetIdentity

{

    [OperationContract]

    string Get(string ClientIdentity);

}

public class GetIdentity : IGetIdentity

{

    public string Get(string ClientIdentity)

    {

        return ("服务端Identity '" + ServiceSecurityContext.Current. PrimaryIdentity.Name +

            "'/n/r客户端Identity '" + ClientIdentity + "'");

    }

}

代码很简单,一个contract提供了一个Get方法,接收一个string参数,返回一个string参数。在后面的测试中,客户端把客户端安全上下文的Identity发送到服务端,服务端返回服务端安全上下文的Identity给客户端。

 

3.1.   客户端验证 – None

这部分的测试代码: NetTcpBinding_Message_None.rar

netTCPBinding绑定的Message安全模式,客户端None验证。此时将使用服务端证书,通过WS-Trust协议建立的安全通道,原理上类似SSLTLS的机制(但不是通过网络传输层来实现,而是通过处理SOAP中的消息)来保证消息的安全性。

这种方式的安全性:

完整性

使用服务端证书,通过WS-Trust协议建立的安全通道

保密性

使用服务端证书,通过WS-Trust协议建立的安全通道

服务端身份身份验证

服务端证书提供

客户端身份验证

没有

 

3.1.1.   获得和安装证书

这里用Makecert.exe工具生成证书,使用下面的命令:

makecert -sr localmachine -ss My -n CN=win2008 -sky exchange -pe -r

这是服务端证书,win2008是服务端的机器名。

如果做过前面BasicHttpBinding的测试,这个服务端证书就应该已经有了。

 

3.1.2.   服务端代码

 

internal static ServiceHost myServiceHost = null;

internal static void Main()

{

    NetTcpBinding myBinding = new NetTcpBinding();

    myBinding.Security.Mode = SecurityMode.Message;

    myBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;

    Uri baseAddress = new Uri("net.tcp://localhost:8056/WCFService/");

    myServiceHost = new ServiceHost(typeof(GetIdentity), baseAddress);

    ServiceEndpoint myServiceEndpoint = myServiceHost.AddServiceEndpoint

        (typeof(IGetIdentity), myBinding, "GetIdentity");

    //设置服务端证书

    myServiceHost.Credentials.ServiceCertificate.SetCertificate("CN=win2008");

    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();

    behavior.HttpGetEnabled = true;

    behavior.HttpGetUrl = new Uri("http://localhost:8057/mex");

    myServiceHost.Description.Behaviors.Add(behavior);

    myServiceHost.Open();

    Console.WriteLine("Service started!");

    Console.ReadLine();

    myServiceHost.Close();

}

 

3.1.3.   客户端代码

 

static void Main(string[] args)

{

    NetTcpBinding myBinding = new NetTcpBinding();

    myBinding.Security.Mode = SecurityMode.Message;

    myBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;

    EndpointAddress ea = new EndpointAddress("net.tcp://win2008:8056/WCFService/GetIdentity");

    GetIdentityClient gc = new GetIdentityClient(myBinding, ea);

    //不验证服务端证书的有效性

    gc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode =

        System.ServiceModel.Security.X509CertificateValidationMode.None;

    //为使用TcpTrace跟踪消息设置的TcpTrace监听端口

    ClientViaBehavior myClientViaBehavior = new ClientViaBehavior

        (new Uri("net.tcp://win2008:8055/WCFService/GetIdentity"));

    gc.Endpoint.Behaviors.Add(myClientViaBehavior);

    //执行代理类Get方法

    string result = gc.Get(WindowsIdentity.GetCurrent().Name);

    Console.WriteLine(result);

    Console.ReadLine();

}

 

3.1.4.   测试

clip_image002

 

 

3.2.   客户端验证 – Windows

这部分的测试代码: NetTcpBinding_Message_Windows.rar

netTCPBinding绑定的Message安全模式,客户端Windows验证。此时将使用服务端证书,通过WS-Trust协议建立的安全通道,原理上类似SSLTLS的机制(但不是通过网络传输层来实现,而是通过处理SOAP中的消息)来保证消息的安全性。

这种方式的安全性:

完整性

使用服务端证书,通过WS-Trust协议建立的安全通道

保密性

使用服务端证书,通过WS-Trust协议建立的安全通道

服务端身份身份验证

服务端证书提供

客户端身份验证

Windows身份验证

 

3.2.1.   获得和安装证书

这里用Makecert.exe工具生成证书,使用下面的命令:

makecert -sr localmachine -ss My -n CN=win2008 -sky exchange -pe -r

这是服务端证书,win2008是服务端的机器名。

如果做过前面BasicHttpBinding的测试,这个服务端证书就应该已经有了。

 

3.2.2.   服务端代码

 

internal static ServiceHost myServiceHost = null;

internal static void Main()

{

    NetTcpBinding myBinding = new NetTcpBinding();

    myBinding.Security.Mode = SecurityMode.Message;

    myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

    Uri baseAddress = new Uri("net.tcp://localhost:8056/WCFService/");

    myServiceHost = new ServiceHost(typeof(GetIdentity), baseAddress);

    ServiceEndpoint myServiceEndpoint = myServiceHost.AddServiceEndpoint

        (typeof(IGetIdentity), myBinding, "GetIdentity");

    //设置服务端证书

    myServiceHost.Credentials.ServiceCertificate.SetCertificate("CN=win2008");

    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();

    behavior.HttpGetEnabled = true;

    behavior.HttpGetUrl = new Uri("http://localhost:8057/mex");

    myServiceHost.Description.Behaviors.Add(behavior);

    myServiceHost.Open();

    Console.WriteLine("Service started!");

    Console.ReadLine();

    myServiceHost.Close();

}

 

3.2.3.   客户端代码

 

static void Main(string[] args)

{

    NetTcpBinding myBinding = new NetTcpBinding();

    myBinding.Security.Mode = SecurityMode.Message;

    myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows;

    EndpointAddress ea = new EndpointAddress("net.tcp://win2008:8056/WCFService/GetIdentity");

    GetIdentityClient gc = new GetIdentityClient(myBinding, ea);

    //不验证服务端证书的有效性

    gc.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode =

        System.ServiceModel.Security.X509CertificateValidationMode.None;

    //为使用TcpTrace跟踪消息设置的TcpTrace监听端口

    ClientViaBehavior myClientViaBehavior = new ClientViaBehavior

        (new Uri("net.tcp://win2008:8055/WCFService/GetIdentity"));

    gc.Endpoint.Behaviors.Add(myClientViaBehavior);

    //执行代理类Get方法

    string result = gc.Get(WindowsIdentity.GetCurrent().Name);

    Console.WriteLine(result);

    Console.ReadLine();

}

 

3.2.4.   测试

clip_image004

可以看出,客户端windows身份被传送到服务端。

 

3.3.   客户端验证 – UserName

这部分的测试代码: NetTcpBinding_Message_UserName.rar

netTCPBinding绑定的Message安全模式,客户端使用UserName验证。此时将使用服务端证书,通过WS-Trust协议建立的安全通道,原理上类似SSLTLS的机制(但不是通过网络传输层来实现,而是通过处理SOAP中的消息)来保证消息的安全性。

这种方式的安全性:

完整性

<span style="font-family: 宋体; font-size: 10pt; m

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值