許多的公司都有自己的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: <?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></xsd:schema></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服務端支持纔行。