WCF 第八章 安全 使用Windows集成安全的安全服务
2011-01-22 19:10 DanielWise 阅读(710) 评论(1) 编辑 收藏 举报在这一部分,我们将主要介绍当向一个组织或者其他安全环境部署和使用服务的问题以及遇到的情况。由于服务可能被宰一个Windows网络的另外机器调用,我们可以利用非基于因特网部署原有的共享认证和授权系统的优势。
因为我们在一个本地网络中,我们可以利用绑定类型的优势,比如TCP(NetTcpBinding),命名管道(如果在同一台机器)来改进性能和效率。我们也可以采用可信赖架构比如MSMQ(NetTcpBinding).
章节示例介绍
这一章用模型来显示的例子有基于WCF的服务和调用者,它们在一个企业防火墙后的局域网间通信。我们按照基本模型来搭建拓扑,有一个契约/实现类库,控制台宿主应用(SampleHost),和客户端控制台应用(ClientConsole).拓扑结构在图片8.2显示,客户端,宿主和其他资源比如数据库都在一个企业防火墙后面并被从开发互联网中分离开。
服务实现是SampleService,它有一个ISamples.cs定义了三个简单操作,GetSecretCode,GetMemberCode和GetPublicCode 在列表8.17中显示。
列表8.17 ISamples.cs 服务契约接口
1 2 3 4 5 6 7 8 9 10 11 12 13 | namespace SampleService { [ServiceContract] public interface ISamples { [OperationContract] string GetSecretCode(); [OperationContract] string GetMemberCode(); [OperationContract] string GetPublicCode(); } } |
ISamples 接口由列表8.18现实的Samples.cs类实现。
列表8.18 Samples.cs 服务实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | namespace SampleService { public class Samples : ISamples { #region ISamples 成员 public string GetSecretCode() { DisplaySecurityDetails(); return "The Secret Code" ; } public string GetMemberCode() { DisplaySecurityDetails(); return "The Member-Only Code" ; } public string GetPublicCode() { DisplaySecurityDetails(); return "The Public Code" ; } #endregion private static void DisplaySecurityDetails() { Console.WriteLine( "Windows Identity = " + WindowsIdentity.GetCurrent().Name); Console.WriteLine( "Thread CurrentPrincipal Identity = " + Thread.CurrentPrincipal.Identity.Name); Console.WriteLine( "ServiceSecurityContext Primary Identity = " + ServiceSecurityContext.Current.PrimaryIdentity.Name); Console.WriteLine( "ServiceSecurityContext Windows Identity = " + ServiceSecurityContext.Current.WindowsIdentity.Name); } } } |
我们已经在Windows中创建了两个将在下一些例子中使用的本地测试账户。使用计算机管理控制台并打开本地用户和组节点。在用户节点下,创建两个账户。在我们的例子中,我们创建了Peter Admin(用户名"peter")和Jessica Member(用户名"jessica").
使用Windows证书认证用户
让我们由查看一个使用Windows证书认证的基于TCP服务的默认行为。服务使用NetTcpBinding配置,像在列表8.19中显示的那样。注意我们已经为了生存代理使能元数据暴露功能。
列表8.19 使用默认安全配置的TCP服务配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <? xml version="1.0" encoding="utf-8" ?> < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior name="ServiceBehavior"> < serviceMetadata httpGetEnabled="true" /> </ behavior > </ serviceBehaviors > </ behaviors > < services > < service behaviorConfiguration="ServiceBehavior" name="SampleService.Samples"> < endpoint address="" binding="netTcpBinding" bindingConfiguration="" name="netTcp" contract="SampleService.ISamples" /> < endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" name="mex" contract="IMetadataExchange" /> < host > < baseAddresses > < add baseAddress="http://localhost:8080/Samples" /> < add baseAddress="net.tcp://localhost:8090/Samples" /> </ baseAddresses > </ host > </ service > </ services > </ system.serviceModel > </ configuration > |
ClientConsole应用程序简单地创建一个生的代理类的实例并顺序对每个操作进行调用,在列表8.20中显示。
列表8.20 ClienConsole应用程序通过TCP调用SamplesService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | namespace Client { class Program { static void Main( string [] args) { Console.WriteLine( "Press ENTER to make service call." ); Console.ReadLine(); Samples.SamplesClient proxy = new Client.Samples.SamplesClient( "netTcp" ); try { Console.WriteLine(proxy.GetPublicCode()); Console.WriteLine(proxy.GetMemberCode()); Console.WriteLine(proxy.GetSecretCode()); } catch (Exception ex) { Console.WriteLine( "exception = " + ex.Message); } Console.ReadLine(); } } } |
当ClientConsole和SampleHost一起运行时,每次调用都成功完成且SampleHost把身份细节写到它的控制台(通过DisplaySecurityDetails方法)。所有身份都报告为运行ClientConsole应用程序的Windows用户。这是期待的结果因为我们还没有介绍任何其他身份。
作者:DanielWise
出处:http://www.cnblogs.com/danielWise/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述