WCF安全参数正确设置方式解读

开发 开发工具
我们在这篇文章中为大家提供了三种不同的WCF安全参数的设置方式,其中就包括:安全方式,消息保护方式以及身份验证方式。

WCF开发工具的推出,对开发人员来说带来了非常不一般的使用体验。那么今天在这篇文章中,我们将会为大家详细介绍一下有关WCF安全参数的正确设置方法,希望能够对又需要的朋友有所帮助。

1. 安全方式

通过设置 Binding 的属性 Security 来实现WCF安全参数的设置。

  1. NetTcpBinding binding = new NetTcpBinding();  
  2. binding.Security.Mode = SecurityMode.Transport;  
  3. binding.Security.Transport.ProtectionLevel = 
    System.Net.Security.ProtectionLevel.EncryptAndSign; 

2. 消息保护

通过 ServiceContractAttribute 和 OperationContractAttribute 特性的 ProtectionLevel 参数我们可以设置不同的消息保护级别。

  1. [ServiceContract(ProtectionLevelProtectionLevel = 
    ProtectionLevel.EncryptAndSign)]  
  2. interface IMyContract  
  3. {  
  4. ...  

3. 身份验证

不同的部署环境,会采取不同的选择来进行WCF安全参数的设置。在 Intranet 环境下,我们可能选择 Windows 集成验证方式,而在 Internet 环境下通常的方案是采取 X.509 数字证书,当然最最通用最最常见依然是用户名/密码。

以 Windows 集成验证为例,客户端可以通过 ClientBase.ClientCredentials 属性向服务器端发送与其相匹配的身份验证信息。缺省情况下,客户端使用当前 Windows 登录账户作为身份验证信息,我们也可以显式设置不同的身份信息。

代理方式:

  1. NetworkCredential credentials = new NetworkCredential( );  
  2. credentials.Domain = "MyDomain";  
  3. credentials.UserName = "MyUsername";  
  4. credentials.Password = "MyPassword";  
  5. using (MyContractClient client = new MyContractClient())  
  6. {  
  7. client.ClientCredentials.Windows.ClientCredential = credentials;  
  8. client.MyMethod( );  

工厂方式:

  1. ChannelFactory<IMyContract> factory = new ChannelFactory<IMyContract>("");  
  2. factory.Credentials.Windows.ClientCredential = new NetworkCredential(...);  
  3. IMyContract client = factory.CreateChannel( );  
  4. using(client as IDisposable)  
  5. {  
  6. client.MyMethod( );  

在服务中,我们可以用 ServiceSecurityContext.Current (或者 OperationContext.Current.ServiceSecurityContext) 来获取相关身份信息。

  1. Console.WriteLine(ServiceSecurityContext.Current.
    WindowsIdentity.AuthenticationType);  
  2. Console.WriteLine(ServiceSecurityContext.Current.
    WindowsIdentity.Name); 

以上就是我们介绍的WCF安全参数的设置方法。

【编辑推荐】

  1. WCF程序事务相关定义与实现方法详解
  2. WCF启用端口三步骤实现
  3. WCF传送二进制流数据基本实现步骤详解
  4. 寄宿WCF服务相关实现方法解析
  5. WCF套接字连接中断具体解决方法详解
责任编辑:曹凯 来源: CSDN
相关推荐

2010-03-02 16:05:48

WCF端点配置

2010-03-01 14:08:53

WCF编码器

2010-02-26 14:05:57

WCF通信方式

2010-02-25 13:54:48

WCF安全参数

2010-02-24 10:07:48

WCF跨越边界

2010-03-02 09:32:54

WCF服务消息

2010-02-22 14:09:08

WCF Dispose

2010-03-01 16:59:31

WCF异常调试

2010-03-01 10:26:40

WCF异步服务

2010-02-26 15:24:27

WCF释放服务对象

2010-03-01 17:39:07

WCF Address

2010-03-01 15:51:01

WCF限流

2009-12-21 18:46:50

WCF传输大数据

2010-03-03 17:33:52

Python读取XML

2010-02-23 10:51:32

WCF Address

2009-12-08 18:14:53

WCF Service

2009-12-08 14:28:16

XTemplate +

2009-12-21 18:32:22

关闭WCF链接

2010-02-22 10:42:12

WCF Stream

2010-02-22 14:28:35

WCF实现loadin
点赞
收藏

51CTO技术栈公众号