構建安全的Xml Web Service系列(一)

來源:互聯網
上載者:User
web|xml|安全

  Xml Web Service 從誕生那天就說自己都麼都麼好,還津津樂道的說internet也會因此而進入一個新紀元,可5年多來,Xml Web Service並沒有像當初宣揚的那樣火起來,儘管在一些領域之內,也有人牛刀小試,但從整體而言,Service還並沒有得到廣泛的應用,原因有很多,有一些來源於目前各大廠商都堅持自己的service標準,不能形成統一,也有對現有的穩定系統不願變更的原因,但還包括web service本身的原因,最明顯的應該是兩個:1) 安全,2)效能。畢業設計的時候,寫的是高效能web service的開發和應用,下面,我想用幾篇文章來闡述一下有關xml web service安全的幾個解決方案。歡迎各位大蝦來砸。

  如何解決網路服務的安全問題,我主要從以下兩個層面進行分析:

  1) 確保調用者的合法身份-保證來源的合法

  2) 在傳輸中不被非法監聽和篡改。

  當然還會有其他方面的安全隱患,希望大家能多多提出,我也好能進一步總結。

  如果您想更快的掌握本文提到的技術,您以前必須瞭解xml web service的工作原理,並且親自開發並部署或者使用過Xml web service,只是您並不相信您部署的xml web service是安全的。

  本節先介紹一種最為簡單的確保調用者合法的解決方案-將使用者名稱和密碼附加在Soap訊息頭部,在伺服器端進行使用者名稱密碼驗證。這種方式從解決了原網路服務不能針對特定對象產生響應的問題。但因為仍以明文格式

  傳輸,所以不能有效地防止資訊在傳輸過程中被偷窺,篡改或偽造。

  如果您以前已經使用了這種方法,請略過此篇文章,我下篇文章中將講述其他方式,更加合理的解決方案,歡迎您繼續關注。

  下面是實現此種解決方案的步驟,請您一步一步來

  第一步:首先您需要建立一個Xml Web Service的服務項目,建立方法如下

  開啟visual studio 2005,在起始頁上點擊建立項目,選擇visual C#中的Asp.Net web 服務應用程式,輸入項目名稱

  第二步:在該項目中建立一個擴充的SoapHeader對象MySoapHeader,如下

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services.Protocols;

namespace WebService1
{
    public class MySoapHeader:SoapHeader
    {
        private string _userName;
        private string _pwd;
        /**//// <summary>
        /// 使用者名稱
        /// </summary>
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }
        /**//// <summary>
        /// 密碼
        /// </summary>
        public string Pwd
        {
            get
            {
                return _pwd;
            }
            set
            {
                _pwd = value;
            }
        }
    }
}

  第三步:建立一個Xml Web Service,另添加一個要求使用SoapHeader的網路服務方法

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace WebService1
{
    /**//// <summary>
    /// Service1 的摘要說明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        public MySoapHeader header = new MySoapHeader();       
        [WebMethod]
        [SoapHeader("header")]      
        public string HelloWorld()
        {
            if (header == null)
            {
                return "您沒有設定SoapHeader,不能正常訪問此服務!";
            }
            if (header.UserName != "jillzhang" || header.Pwd != "123456")
            {
                return "您提供的身分識別驗證資訊有誤,不能正常訪問此服務!";
            }
            return "Hello World";
        }
    }
}

  第四步:建立一個調用Xml Web Service的Console應用程式,如下:

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

namespace ConsoleApplication1
{
    class Program
    {
      
        static void Main(string[] args)
        {
            localhost.Service1 ws = new ConsoleApplication1.localhost.Service1();
            //ws.MySoapHeaderValue = new ConsoleApplication1.localhost.MySoapHeader();
            //ws.MySoapHeaderValue.UserName = "jillzhang";
            //ws.MySoapHeaderValue.Pwd = "123456";
            Console.WriteLine(ws.HelloWorld());
        }
    }
}

  下面的分析,對於大家來說,應該是最重要的,很多人不清楚SoapHeader的工作原理,為什麼這麼怪異的寫法竟能產生神奇的效果,下面我將不同情形下的Soap訊息解析出來,大家仔細觀察這個資訊,並可以清晰地掌握了SoapHeader的工作原理了.

  首先,先看看沒有設定SoapHeader的情況下,Soap訊息為:

-----Soap請求 在 2007年05月22日 12時39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>

-----Soap響應 在 2007年05月22日 12時39分40秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>您提供的身分識別驗證資訊有誤,不能正常訪問此服務!</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>

  再看看在設定了SoapHeader之後的Soap的請求和響應資訊

-----Soap請求 在 2007年05月22日 12時42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><MySoapHeader xmlns="http://tempuri.org/"><UserName>jillzhang</UserName><Pwd>123456</Pwd></MySoapHeader></soap:Header><soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>

-----Soap響應 在 2007年05月22日 12時42分20秒
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorldResponse xmlns="http://tempuri.org/"><HelloWorldResult>Hello World</HelloWorldResult></HelloWorldResponse></soap:Body></soap:Envelope>

  點正是通過這個節點,SoapMessage將資訊傳遞給了網路服務端,網路服務端便可以從中解析出來,並加以處理,從上面的SoapMessage中,我們也看出,使用者名稱和密碼是以明文的格式傳輸的,這樣,SoapHeader就更像Http協議中的Cookie了,我們可以參考Cookie的使用,來擴充SoapHeader,讓它變得更加安全些,但總的看來,通過直接設定SoapHeader的方法提高安全性還是有一定限制的。在安全不是特別重要的應用情形中,推薦採用此種解決方案,因為它方便快捷,靈活易用。

  下一節,我將介紹一下,如何擷取SoapMessage.



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.