加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice的验证

发布时间:2020-12-17 02:50:54 所属栏目:安全 来源:网络整理
导读:许多的公司都有自己的web服务来支撑自己系统内的运营逻辑,并且是非公开的,那么如何对自己的web服务进行验证呢?不可能任何一个知道你的webservice? url 的人都可以去调用你的服务,那企业内部那么多数据岂不全被剽窃?我在这开头只是言明web服务验证的重要

许多的公司都有自己的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 OKContent-Type: text/xml; charset=utf-8Content-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服务端支持才行。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读