WebService基于SoapHeader实现安全认证
本文仅提供通过设置SoapHeader来控制非法用户对WebService的调用,如果是WebService建议使用WSE3.0来保护 以下提供一种基于SoapHeader的自定义验证方式。
1.首先要自定义SoapHeader,须继承System.Web.Services.Protocols.SoapHeader 。 using System;
using System.Collections.Generic;
using System.Web;
///
需要添加的soapheader <soapenv:Header> <tns:RequestSOAPHeader xmlns:tns="http://sys.webservice.client"> } } import java.util.List;import javax.xml.namespace.QName;import org.apache.cxf.binding.soap.SoapHeader root.appendChild(child); QName qname = new QName("RequestSOAPHeader"); SoapHeader head = new SoapHeader(qname, root); List
支持通过Http请求方法调用webservice,同时支持SoapHeader验证。 using System.Web.Services.Protocols;
namespace Globalegrow.Service
{
///
1.NET中撰写需要使用SoapHeader验证的Web Service 代码1:WebService.cs using System.Web.Services; using System.Web.Services.Protocols public WebService () {} public AuthenticationInfo authenticationInfo; [WebMethod] [SoapHeader ("authenticationInfo")] //为服务添加SoapHeader public string HelloWorld() { if AuthenticationInfo 的摘要说明 /// public class AuthenticationInfo:System.Web.Services.Protocols.SoapHeader public var password:String; } } 代码6:Authentication.as package{ import mx.rpc.soap.SOAPHeader
SoapHeader SoapHeader 多数情况下用来传递用户身份验证信息,当然它的作用远不止如此,有待于在实际应用中发掘。 SoapHeader 使用步骤: (1) 创建继承自 System.Web.WebServices.SoapHeader 的自定义 SoapHeader 类型。 (2) 在 WebService 中创建拥有 public 访问权限的自定义 SoapHeader 字段。 (3) 在需要使用 SoapHeader 的 WebMethod 上添加 SoapHeaderAttribute 访问特性。 (4) 生成器会自动为客户端生成同名的自定义 SoapHeader 类型,只不过比起我们在 WebService 端创建的要复杂一些。
SoapFault — SoapFault constructor SoapFault::__toString — Obtain a string representation of a SoapFault SoapHeader SoapHeader::__construct — SoapHeader constructor SoapHeader::SoapHeader — SoapHeader constructor SoapParam
The property schema must have the target namespace of http://schemas.microsoft.com/BizTalk/2003/SOAPHeader 必须定义一个property schema 并且namespace必须是http://schemas.microsoft.com/BizTalk/2003/SOAPHeader,还有定一个element
(强烈推荐) 4、通过WebService给我们提供的SoapHeader来实现用户身份验证。 (个人推荐使用:③+④两个方案进行组合使用)(强烈推荐) 总结:我们可以通过SoapHeader来保证WebService的授权使用;可以通过SSL来保证数据的加密,防止网络侦听! 五、C# WebService如何采用SoapHeader进行身份验证 ?
Header身份验证
1、我们实现一个用于身份验证的类,文件名MySoapHeader.cs
MySoapHeader类继承自System.Web.Services.Protocols.SoapHeader System.Web.Services.Protocols;
///
// <---- return request; } 如果需要在soap header加入内容: 从System.Web.Services.Protocols.SoapHeader 类继承,定义自己的soapheader类:MySoapHeader 在代理类中增加属性: private MySoapHeader mySoapHeaderValueField; public
其中有个方法 introduceAcceptedBusinessByStandardInterface(string $xmlStr),将是开发文档中提到的要使用的接口,参数为xml字符串 另外有的接口中提到有SoapHeader
/soap.xilen.com/", "check", "ns"); body.addBodyElement(soapQname); /** * 添加Header */ SOAPHeader
void public __setLocation ( string $location = "" ) : string|null public __setSoapHeaders ( SoapHeader headers = null ) : bool public __soapCall ( string $name , array $args , array|null $options = null , SoapHeader