Web Services Enhancements 3.0 Quick Start(四)

        主要是详细介绍使用WSE3.0建立使用安全的WebService
一、下载WSE3.0,并安装,特别要提的是选择安装的时候选择vs2005开发人员的模式  下载
二、一些理论的知识,可参考控制台关于安全证书的帮助,了解一些必要的基础知识
三、了解两个工具
certmgr.exe    http://msdn2.microsoft.com/zh-cn/library/bfsktky3.aspx
makecert.exe http://msdn2.microsoft.com/zh-cn/library/e78byta0.aspx
四、建立服务器和客户端的证书,在下载示例中(Setup.bat)注意应使用“Visual Studio 2005 命令提示”
五、建立一个WebService和一个客户端调用控制台程序
六、设置WebService配置文件
1、在WebService工程中右键选择WSE settings
2、选择“Enable this for Web Service Enhancement”和“Enable Microsoft Web Service EnhanCement Soap Protocol Factory”
3、选择Policy tab 选择 Enable Policy 并点击Add..
4、填写Policy名称,下一步
5、选择“Secure a service application” 在选择客户端访问方式中选择“Username”下一步
6、选择“Sign and Encrypt” 下一步
7、选择“Select Certificate..”
8、选择“WSEQuickStartServer”证书,下一步
9、完成
七、就能生成web.config和wse3policyCache.config
八、添加 Microsoft.Web.Services3.dll的引用,并在WebSerice类中添加[Policy("ServicePolicy")]
九、在App_Code中添加CustomUsernameTokenManager类,实现UsernameTokenManager接口

None.gif using  System;
None.gif
using  System.Xml;
None.gif
using  System.Security.Permissions;
None.gif
None.gif
using  Microsoft.Web.Services3.Security;
None.gif
using  Microsoft.Web.Services3.Security.Tokens;
ExpandedBlockStart.gifContractedBlock.gif
/**/ /// 
InBlock.gif
/// UsernameTokenManager 的摘要说明
ExpandedBlockEnd.gif
/// 

None.gif public   class  UsernameTokenManager : UsernameTokenManager
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public UsernameTokenManager()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public UsernameTokenManager(XmlNodeList nodes)
InBlock.gif        : 
base(nodes)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected override string AuthenticateToken(UsernameToken token)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
byte[] password = System.Text.Encoding.UTF8.GetBytes(token.Username);
InBlock.gif
InBlock.gif        Array.Reverse(password);
InBlock.gif
InBlock.gif        
return Convert.ToBase64String(password);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

十、实现Web Service

None.gif using  System;
None.gif
using  System.Web;
None.gif
using  System.Web.Services;
None.gif
using  System.Web.Services.Protocols;
None.gif
None.gif
using  Microsoft.Web.Services3;
None.gif
None.gif[WebService(Namespace 
=   " http://tempuri.org/ " )]
None.gif[WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
None.gif[Policy(
" ServicePolicy " )]
None.gif
public   class  Service : System.Web.Services.WebService
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public Service () dot.gif{
InBlock.gif
InBlock.gif        
//如果使用设计的组件,请取消注释以下行 
InBlock.gif        
//InitializeComponent(); 
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    [WebMethod]
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public string HelloWorld() dot.gif{
InBlock.gif        
return "Hello World";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}

None.gif


十一、在客户项目中添加Microsoft.Web.Services3.dll的引用,并添加WebService引用
十二、其中查看Reference.cs中Service中是否继承 Microsoft.Web.Services3.WebServicesClientProtocol
1、选择客户项目右键选择WSE settings
2、选择Policy tab 选择 Enable Policy 并点击Add..
3、填写Policy名称,下一步
4、选择“Secure a client applition”并选择“Username”并下一步
5、选择“sign and Encrypt”下一步
6、选择“Select Certificate...”并选择"WSE2QuickStartServer"下一步并完成就能生成web.config和wse3policyCache.config
十三、在客户端中完成以下

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
using  Emp.localhost;
None.gif
None.gif
using  Microsoft.Web.Services3;
None.gif
using  Microsoft.Web.Services3.Design;
None.gif
using  Microsoft.Web.Services3.Security;
None.gif
using  Microsoft.Web.Services3.Security.X509;
None.gif
using  Microsoft.Web.Services3.Security.Tokens;
None.gif
None.gif
None.gif
namespace  Emp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [Policy(
"ClientPolicy")]                                                            //把“ClientPolicy”做为元数据定义
InBlock.gif
    class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ServiceWse sw 
= new ServiceWse();                                           //不是Service而是ServiceWse,
InBlock.gif
            UsernameToken token = null;                                                 //定义UsernameToken
InBlock.gif
            bool useCorrectPassword = true;                                             // change to false, and the call will fail
InBlock.gif
            string username = Environment.UserName;                                     //获取当前线程的用户 (你也可以自己定义)
InBlock.gif
            byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(username);        //用户名的UTF8的格式 
InBlock.gif
            Array.Reverse(passwordBytes);
InBlock.gif
InBlock.gif            
if (useCorrectPassword)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string passwordEquivalent = Convert.ToBase64String(passwordBytes);      //密码
InBlock.gif
                token = new UsernameToken(username, passwordEquivalent);                //设置用户名和密码
ExpandedSubBlockEnd.gif
            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                token 
= new UsernameToken(username, "BadPassword");
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            sw.SetClientCredential(token);                                             
//提交客户端的令牌
InBlock.gif
            sw.SetPolicy("ClientPolicy");                                              //设置客户端证书
InBlock.gif

InBlock.gif            Console.WriteLine(
"Calling {0}", sw.Url);
InBlock.gif            Console.WriteLine(sw.HelloWorld());
InBlock.gif            Console.WriteLine(
"Press [Enter] to continuedot.gif");
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
十四、总结
主要讲述利用WSE3.0构建安全的WebService,可以看出WSE3.0主要是建立配置文件和加入元数据就能实现安全,使用WSE3.0以一种比较优美的模式来完成WebService的安全,达到业务逻辑和安全分开,可以对现有的或者新项目平滑的达到WebService的安全

源码下载 

转载于:https://www.cnblogs.com/jiekeng/archive/2006/11/08/554545.html

你可能感兴趣的:(Web Services Enhancements 3.0 Quick Start(四))