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

成都创新互联公司基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业电信内江机房,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。
1. 安全方式
通过设置 Binding 的属性 Security 来实现WCF安全参数的设置。
- NetTcpBinding binding = new NetTcpBinding();
- binding.Security.Mode = SecurityMode.Transport;
- binding.Security.Transport.ProtectionLevel =
System.Net.Security.ProtectionLevel.EncryptAndSign;
2. 消息保护
通过 ServiceContractAttribute 和 OperationContractAttribute 特性的 ProtectionLevel 参数我们可以设置不同的消息保护级别。
- [ServiceContract(ProtectionLevelProtectionLevel =
ProtectionLevel.EncryptAndSign)]- interface IMyContract
- {
- ...
- }
3. 身份验证
不同的部署环境,会采取不同的选择来进行WCF安全参数的设置。在 Intranet 环境下,我们可能选择 Windows 集成验证方式,而在 Internet 环境下通常的方案是采取 X.509 数字证书,当然最最通用最最常见依然是用户名/密码。
以 Windows 集成验证为例,客户端可以通过 ClientBase.ClientCredentials 属性向服务器端发送与其相匹配的身份验证信息。缺省情况下,客户端使用当前 Windows 登录账户作为身份验证信息,我们也可以显式设置不同的身份信息。
代理方式:
- NetworkCredential credentials = new NetworkCredential( );
- credentials.Domain = "MyDomain";
- credentials.UserName = "MyUsername";
- credentials.Password = "MyPassword";
- using (MyContractClient client = new MyContractClient())
- {
- client.ClientCredentials.Windows.ClientCredential = credentials;
- client.MyMethod( );
- }
工厂方式:
- ChannelFactory
factory = new ChannelFactory (""); - factory.Credentials.Windows.ClientCredential = new NetworkCredential(...);
- IMyContract client = factory.CreateChannel( );
- using(client as IDisposable)
- {
- client.MyMethod( );
- }
在服务中,我们可以用 ServiceSecurityContext.Current (或者 OperationContext.Current.ServiceSecurityContext) 来获取相关身份信息。
- Console.WriteLine(ServiceSecurityContext.Current.
WindowsIdentity.AuthenticationType);- Console.WriteLine(ServiceSecurityContext.Current.
WindowsIdentity.Name);
以上就是我们介绍的WCF安全参数的设置方法。
【编辑推荐】
- WCF程序事务相关定义与实现方法详解
- WCF启用端口三步骤实现
- WCF传送二进制流数据基本实现步骤详解
- 寄宿WCF服务相关实现方法解析
- WCF套接字连接中断具体解决方法详解
新闻名称:WCF安全参数正确设置方式解读
路径分享:http://www.jxjierui.cn/article/cdgphec.html


咨询
建站咨询
