http://blog.csdn.net/winneraqun/archive/2009/09/11/4542017.aspx

 许多的公司都有自己的web服务来支撑自己系统内的运营逻辑,并且是非公开的,那么如何对自己的web服务进行验证呢?不可能任何一个知道你的webservice  url 的人都可以去调用你的服务,那企业内部那么多数据岂不全被剽窃?我在这开头只是言明web服务验证的重要性,接下来,我将从比较基础的讲起如何使用soapheader来验证。

首先,我们来讲讲什么是soapheader。soap协议是啥我就不好讲了,如果读者还没有明白soap是啥,那阅读这篇文章对你没有用处。soap是由 一个信封,envelop ,一个header,一个body以xml的格式组织而成。请看如下的soap格式,你就明白soap的组成。

<?xml version="1.0" encoding="utf-8"?>

//这个就是soap的信封,也是soap协议的根节点。
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
这个就是头了,注意看头里面的信息,下面会提到。 

<soap:Header>
    <UserAuthenticationKey xmlns="http://www.com/UFAService/">
      <key>string</key>
    </UserAuthenticationKey>
  </soap:Header>

//这个就是soap协议的体了。这个是你请求这个web方法所带进来的参数,入参。并且表明这些参数的性质,如果是枚举,还会挨个列举出来哦
  <soap:Body>

//这个请求的是Search这个web方法,
    <Search xmlns="http://www.com/UFAService/">
      <PartnerCD>int</PartnerCD>
      <GetOrders>boolean</GetOrders>
      <GetCustomers>boolean</GetCustomers>
      <GetStoredValueAccounts>boolean</GetStoredValueAccounts>
      <GetPaymentDevices>boolean</GetPaymentDevices>
      <CustomerNumber>string</CustomerNumber>
      <FirstName>string</FirstName>
      <LastName>string</LastName>
      <PostalCode>string</PostalCode>
      <EmailAddress>string</EmailAddress>
      <MDN>string</MDN>
      <PaymentDeviceNumber>string</PaymentDeviceNumber>
      <OrderNumber>string</OrderNumber>
      <ANI>string</ANI>
      <IPAddress>string</IPAddress>
    </Search>
  </soap:Body>
</soap:Envelope>

好了,经过我的注释,应该明白什么是soapheader了吧。下面,还有你成功通过验证后调用了web方法,返回的soap,可以看看。

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SearchResponse xmlns="http://www.com/UFAService/">
      <SearchResult>//返回的时是一个xml格式的dataset
        <xsd:schema>schema</xsd:schema>xml</SearchResult>
    </SearchResponse>
  </soap:Body>
</soap:Envelope>接下来,我们开始在代码中如何实现这个soap协议了。

第一步,新建一个asp.net web服务,建一个类,这个类要继承soapheader这个类,并且有个公有的字段,来用作验证钥匙。代码如下:

public class UserAuthenticationKey : SoapHeader
 {
  private string key;
  public UserAuthenticationKey()
  {
   key = "hello";
  }

  public string Key
  {
   get { return key ; }
   set { key = value ; }
  }

 }

 然后,开始写web方法了。在web方法所在的那个类中,需要使用到继承那个类的对象,我们使用黑箱复用,在web方法所在类加入一个他的对象即可实现。还有一个不要忘记的是在web方法上要标明[SoapHeader("Key")] 这个key就是你要公开去验证的那个。当然,你的判定可以更加复杂,那是你自己的实现问题了。在我这里,只要你传进来的Key是"hello"就能验证通过。

public class MyService : System.Web.Services.WebService
 {
  private UserAuthenticationKey UAkey = new UserAuthenticationKey();
  public UserAuthenticationKey Key
  {
   get { return UAkey; }
   set { UAkey = value ; }
  }

[WebMethod(BufferResponse = true,Description = "欢迎方法" ,CacheDuration = 0,EnableSession=false,
    MessageName = "HelloFriend")]
  [SoapHeader("Key")]
  public string Welcome(string  username)
  {
   if(this.Key.Key=="hello")
   return "Welcome " + username;
   else
    return"wrong!";
  }

最后,该说说怎么写客户端的调用了。这个是非常有学问的了。下面的代码,是客户端创建web服务对象的代码,先看,我再解释。

public static UFAService CreateWebServiceInstance()
  {
   UFAService proxy = new UFAService();
   string timeout = ConfigurationSettings.AppSettings["WebServiceTimeout"];
   if ( timeout != null && !timeout.Equals(String.Empty) )
   {
    proxy.Timeout = Convert.ToInt32(timeout);
   }
   string ufaServiceURL = ConfigurationSettings.AppSettings["UFAServiceURL"];
   if ( ufaServiceURL != null && !ufaServiceURL.Equals(String.Empty) )
   {
    proxy.Url = ufaServiceURL;
   }
   UserAuthenticationKey key = new UserAuthenticationKey();
   key.key = ComputeHash();
//注意这个computHash这个方法,非常重要,
   proxy.UserAuthenticationKeyValue = key;//还有这个UserAuthenticationKeyValue是怎么来的呢?原来是系统在web对象上添加的这个字段的值
   proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                 
   return proxy;
  }

这些步骤差不多都比较容易理解,关键是如何对字段进行加密,你可以采用多种方式来定义computHash这个方法来返回这个字符串,然后再传进web服务的时候进行复杂的验证过程,比如把你机器的AD(active directory)号码,加上当前的日期,或者加上你的座机电话等等,再进行md5加密,再用指定的方法再包装。。。当然,所有的工作必须要你的web服务端支持才行。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xxqq0824/archive/2006/04/25/676105.aspx

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/winneraqun/archive/2009/09/11/4542017.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值