惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hackread – Cybersecurity News, Data Breaches, AI and More
W
WeLiveSecurity
C
Check Point Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
A
Arctic Wolf
NISL@THU
NISL@THU
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
M
MIT News - Artificial intelligence
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
G
GRAHAM CLULEY
O
OpenAI News
S
Schneier on Security
Google Online Security Blog
Google Online Security Blog
Vercel News
Vercel News
宝玉的分享
宝玉的分享
Attack and Defense Labs
Attack and Defense Labs
T
The Blog of Author Tim Ferriss
量子位
aimingoo的专栏
aimingoo的专栏
The Cloudflare Blog
P
Privacy & Cybersecurity Law Blog
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 热门话题
博客园_首页
F
Full Disclosure
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
U
Unit 42
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News - Newest:
Hacker News - Newest: "LLM"
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Scott Helme
Scott Helme
TaoSecurity Blog
TaoSecurity Blog

博客园 - jiekengxu

工厂方法模式(Factory Method Pattern) 生成器模式(Builder) 抽象工厂模式(Abstract Factory Pattern) asp.net 2.0 权限树的控制(多处转载) 中小型项目的权限管理的数据关系图 简陋的会计凭证金额输入控件(再加强) 初识HTC asp.net 2.0 缓存(页面输出缓存) asp.net 2.0 缓存(理论篇) 智能客户端概述 Web Services Enhancements 3.0 Quick Start(三) 简陋的会计凭证金额输入控件(加强) Web Services Enhancements 3.0 Quick Start(二) 简陋的会计凭证金额输入控件 Web Services Enhancements 3.0 Quick Start(一) 利用AJAX.net设计现在执行运算的报表 单件模式在报表中的使用 Web Service Software Factory 构架 Web Service Software Factory 入门
Web Services Enhancements 3.0 Quick Start(四)
jiekengxu · 2006-11-08 · via 博客园 - jiekengxu

        主要是详细介绍使用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接口

using System;
using System.Xml;
using System.Security.Permissions;

using Microsoft.Web.Services3.Security;
using Microsoft.Web.Services3.Security.Tokens;
/// <summary>
/// UsernameTokenManager 的摘要说明
/// </summary>

public class UsernameTokenManager : UsernameTokenManager
{
    
public UsernameTokenManager()
    
{
    }

    
public UsernameTokenManager(XmlNodeList nodes)
        : 
base(nodes)
    
{
    }

    
protected override string AuthenticateToken(UsernameToken token)
    
{

        
byte[] password = System.Text.Encoding.UTF8.GetBytes(token.Username);

        Array.Reverse(password);

        
return Convert.ToBase64String(password);
    }

}

十、实现Web Service

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using Microsoft.Web.Services3;

[WebService(Namespace 
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[Policy(
"ServicePolicy")]
public class Service : System.Web.Services.WebService
{
    
public Service () {

        
//如果使用设计的组件,请取消注释以下行 
        
//InitializeComponent(); 
    }


    [WebMethod]
    
public string HelloWorld() {
        
return "Hello World";
    }

    
}


十一、在客户项目中添加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
十三、在客户端中完成以下

using System;
using System.Collections.Generic;
using System.Text;

using Emp.localhost;

using Microsoft.Web.Services3;
using Microsoft.Web.Services3.Design;
using Microsoft.Web.Services3.Security;
using Microsoft.Web.Services3.Security.X509;
using Microsoft.Web.Services3.Security.Tokens;


namespace Emp
{
    [Policy(
"ClientPolicy")]                                                            //把“ClientPolicy”做为元数据定义
    class Program
    
{
        
static void Main(string[] args)
        
{
            ServiceWse sw 
= new ServiceWse();                                           //不是Service而是ServiceWse,
            UsernameToken token = null;                                                 //定义UsernameToken
            bool useCorrectPassword = true;                                             // change to false, and the call will fail
            string username = Environment.UserName;                                     //获取当前线程的用户 (你也可以自己定义)
            byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(username);        //用户名的UTF8的格式 
            Array.Reverse(passwordBytes);

            
if (useCorrectPassword)
            
{
                
string passwordEquivalent = Convert.ToBase64String(passwordBytes);      //密码
                token = new UsernameToken(username, passwordEquivalent);                //设置用户名和密码
            }

            
else
            
{
                token 
= new UsernameToken(username, "BadPassword");
            }


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

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

    }

}

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

源码下载