上两章主要是说明了利用Web Services Enhancements 3.0创建安全策略.现在主要是通过一个示例来较完整的讲述Web Services Enhancements 3.0构建安全Web Services
        在 June CTP 中共有六种交钥匙声明第一种是 AnonymousOverCertificateSecurity,其中 Web 服务通过其 X.509 证书对其本身进行到调用者的验证。所有调用者均是匿名的。通过使用服务器公钥交换的对称密钥提供消息级的完整性和保密性。在逻辑上,此模型与您访问喜欢的网上书店并购买某本书时所发生的情况类似。书店的客户太多了,不可能对每个客户逐一验证,但接受信用卡号和帐单接收地址是很好的方法,这样这笔交易的验证责任就转给银行了。在许多企业对企业的 Web 服务情况中,客户机和服务器都分配有证书。这种情况下,您可以从以下两种声明选择一种:CertificateMutualAuthenticationProfile 或 MutualCertificateSecurity。它们在逻辑上非常相似,客户机和服务器通过彼此出示 X.509 证书进行验证,并证明相应私钥的所有权。通过使用服务器公钥交换的对称密钥提供消息级的完整性和保密性。第一种声明依赖于现有的 WS-Security 1.0 规范,而第二种则使用 WS-Security 1.1 规范草案,其中包括一些新增功能(例如加密的 SOAP 标头)。虽然使用证书对客户机进行验证是个不错的选择,通常这样做简单但却不可行,因为这增加了维护(发放和管理客户机证书所必需的)公钥基础结构 (PKI) 的开销。通常,通过简单的用户名和密码对客户机进行验证更加合理。以下是支持此类验证的两种交钥匙声明:UsernameOverTransportSecurity 和 UsernameOverCertificateSecurity。如果要依赖 SSL 验证服务器并为通道提供完整性和保密性,则应当使用 UsernameOverTransportSecurity。而当 SSL 不适用时(例如,当有中间方并且要进行端对端验证时),UsernameOverCertificateSecurity 则十分有用。最后,如果要为支持 Kerberos 的 intranet 构建 Web 服务(现代 Windows® 域环境中的情况),KerberosSecurity 声明是非常合适的选择。由于 Kerberos 使用纯传统的加密方法来验证客户机和服务,因此与基于证书的解决方案相比,它不会为服务带来那么大的负担。但迄今为止 Windows 域环境的最大好处是支持单一登录。通过使用客户机的默认登录凭据进行验证,无需查询客户机密码。当然也不需要 PKI。服务器可以使用 Windows 组作为角色为资源访问授权或者只是模拟客户机,并让 Windows 处理访问检查。(转自http://www.microsoft.com/china/MSDN/library/WebServices/WebServices/issues0602WSE30.mspx?mfr=true
        我们现在主要是通过usernameForCertificateSecurity作为示例
首先要介绍两个工具软件makecert.exe和certmgr.exe
makecert.exe是证书创建工具生成仅用于测试目的的 X.509 证书。它创建用于数字签名的公钥和私钥对,并将其存储在证书文件中。此工具还将密钥对与指定发行者的名称相关联,并创建一个 X.509 证书,该证书将用户指定的名称绑定到密钥对的公共部分。(http://msdn2.microsoft.com/zh-cn/library/bfsktky3.aspx详细介绍)
certmgr.exe证书管理器工具管理证书、证书信任列表 (CTL) 和证书吊销列表 (CRL)。(http://msdn2.microsoft.com/zh-cn/library/e78byta0.aspx详细介绍)
        现在我们就使用这两个工具来生成我们使用的工具
Web Services Enhancements 3.0 Quick Start(三)makecert.exe -sr LocalMachine -ss MY -a sha1 -n CN=%WSE2QuickStartServer% -sky exchange -pe
    certmgr.exe -add -r LocalMachine -s My -c -n %WSE2QuickStartServer% -r CurrentUser -s AddressBook
    makecert.exe -sr CurrentUser -ss My -a sha1 -n CN=%WSE2QuickStartClient% -sky exchange -pe
         查看证书
开始->运行。。。MMC
文件->添加/删除管理单元->添加->证书->添加->计算机帐户->个人->证书->WSE2QuickStartServer
文件->添加/删除管理单元->添加->证书->添加->当前用户->个人->证书->WSE2QuickStartClient
文件->添加/删除管理单元->添加->证书->添加->当前用户->其他人->证书->WSE2QuickStartServer
如图
Web Services Enhancements 3.0 Quick Start(三)
程序实现
一、新建一个WebServie,并建立客户端需要包含用户名和密码的Usernametoken的安全令牌的安全策略(http://www.cnblogs.com/jiekeng/archive/2006/10/25/539962.html
信建一个类CustomUsernameTokenManager 继承自Microsoft.Web.Services3.Security.Tokens.UsernameTokenManager
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using System;
Web Services Enhancements 3.0 Quick Start(三)
using System.Xml;
Web Services Enhancements 3.0 Quick Start(三)
using System.Security.Permissions;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3.Security;
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3.Security.Tokens;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
namespace Microsoft.Web.Services3.QuickStart
 
建立一个web服务,返回一个字符串
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using System;
Web Services Enhancements 3.0 Quick Start(三)
using System.Collections.Generic;
Web Services Enhancements 3.0 Quick Start(三)
using System.Web;
Web Services Enhancements 3.0 Quick Start(三)
using System.Web.Services;
Web Services Enhancements 3.0 Quick Start(三)
using System.Web.Services.Protocols;
Web Services Enhancements 3.0 Quick Start(三)
using System.Xml.Serialization;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3.QuickStart;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)[WebService(Namespace 
= "http://stockservice.contoso.com/wse/samples/2005/10")]
Web Services Enhancements 3.0 Quick Start(三)[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
Web Services Enhancements 3.0 Quick Start(三)[Policy(
"ServerPolicy")]
Web Services Enhancements 3.0 Quick Start(三)
public class WSSecurityUsernameService : System.Web.Services.WebService
}

二、新建一个控制台工程,设置WSE(http://www.cnblogs.com/jiekeng/archive/2006/10/26/541210.html(使用Username客户访问方式))
添加服务引用,在代理类中大家看到public partial class WSSecurityUsernameServiceWse : Microsoft.Web.Services3.WebServicesClientProtocol,并不是
System.Web.Services.Protocols.SoapHttpClientProtocol
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using System;
Web Services Enhancements 3.0 Quick Start(三)
using System.Collections.Generic;
Web Services Enhancements 3.0 Quick Start(三)
using System.Text;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3;
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3.Design;
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3.Security;
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3.Security.X509;
Web Services Enhancements 3.0 Quick Start(三)
using Microsoft.Web.Services3.Security.Tokens;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
using localhost;
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
Web Services Enhancements 3.0 Quick Start(三)
namespace WSSecurityUsernameClient
运行就能看到效果,
总结
我现在总有的疑惑的是客户端怎么安装证书的问题,如果有程序来安装,那BS程序就带来了部署上的问题,如果人工导入导出,那不是更郁闷,现在还在研究中,希望有人能多多帮忙啊

相关文章:

  • 2021-07-03
  • 2021-07-13
  • 2021-11-17
  • 2021-12-14
  • 2021-05-12
  • 2021-10-31
  • 2021-12-22
  • 2021-07-22
猜你喜欢
  • 2021-10-18
  • 2021-05-30
  • 2021-06-12
  • 2022-02-26
  • 2022-02-18
  • 2022-03-03
  • 2022-02-20
相关资源
相似解决方案