.NET中 webservice加密簡談(基礎1)

來源:互聯網
上載者:User

標籤:

   在部落格園都呆了幾年了,一直都是看各位大神的文章。自己從來也沒親自寫多點點。本碼農打算從今年去也開始寫寫文章,打打基礎,以備不時の需。廢話不多說了,開始進入正題。高手就可以不用看了。僅是給新人看的。哈哈

  1.     加密目的:webservice最後部署了無非就是一個URL地址。只要知道地址就能夠看到響應的結果。所以給webservice設定了訪問使用者和密碼。增強安全性,使它只能被授權的使用者使用。
  2. 集體實現過程如下:

        (1):開啟VS2010建立解決方案(WService20150106)(PageBaseWebservice.asmx)(個人覺得VS在大了推薦大家使用WCF,還是什麼其他原因沒有在建立的時候直接找到webservice項目,可以先建立一個空的ASP.net 項目,在添加就能找到webservice)。代碼如下注釋很清楚。

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Services; 6  7 namespace Webservice2015 8 { 9     /// <summary>10     /// PageBaseWebservice 的摘要說明11     /// </summary>12     [WebService(Namespace = "http://tempuri.org/")]13     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]14     [System.ComponentModel.ToolboxItem(false)]15     // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。16     // [System.Web.Script.Services.ScriptService]17     public class PageBaseWebservice : System.Web.Services.WebService18     {19 20         public MySoapHeader _MySoapHeader;// 定義一個變數21         /// <summary>22         ///  重點是這個類是繼承System.Web.Services.Protocols.SoapHeader 這是關鍵。加密就全靠這個了23         /// </summary>24         public class MySoapHeader : System.Web.Services.Protocols.SoapHeader25         {26             #region27             /// <summary>28             /// 使用者名稱29             /// </summary>30             private string _name;31             public string Name32             {33                 get { return _name; }34                 set { _name = value; }35             }36             /// <summary>37             /// 秘密38             /// </summary>39             private string _password;40             public string Password41             {42                 get { return _password; }43                 set { _password = value; }44             }45             #endregion46 47             /// <summary>48             /// 無參數的建構函式49             /// </summary>50             public MySoapHeader()51             {52             }53             /// <summary>54             /// 帶參數的建構函式55             /// </summary>56             /// <param name="Name">參數使用者名稱</param>57             /// <param name="Password">參數秘密</param>58             public MySoapHeader(string Name, string Password)59             {60                 this.Name = Name;61                 this.Password = Password;62             }63         }64 65         [WebMethod(Description = "驗證許可權", CacheDuration = 0, BufferResponse = true)]66         [System.Web.Services.Protocols.SoapHeader("_MySoapHeader")]67         public bool Validation()68         {69             /// 判斷使用者名稱 和密碼 是否正確。 這裡僅做測試。使用者名稱和密碼 可以寫在設定檔中。70             if (_MySoapHeader.Name == "KG" && _MySoapHeader.Password=="123456")71                 // 匹配成功72                 return true;73             //匹配失敗74             return false;75         }76     }77 }
View Code

 

 

 

          (2):在建立一個WebServiceTest.asmx。這個和(1)中PageBaseWebservice.asmx 是繼承關係。PageBaseWebservice 繼承自 System.Web.Services.WebService。WebServiceTest 是繼承PageBaseWebservice。代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace Webservice2015{    /// <summary>    /// WebServiceTest 的摘要說明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [System.ComponentModel.ToolboxItem(false)]    // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。    // [System.Web.Script.Services.ScriptService]    public class WebServiceTest : PageBaseWebservice    {        [WebMethod(Description = "驗證測試", CacheDuration = 0, BufferResponse = true)]        // 加上標記        [System.Web.Services.Protocols.SoapHeader("_MySoapHeader")]        public string HelloWorld()        {            if (Validation())// 驗證            {                return "Hello World";            }            else            {                return "你沒許可權";            }                 }    }}
View Code

        (3):建立一個控制台程式。在添加引用。代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            /// 服務            ServiceReference1.WebServiceTestSoapClient webclient = new ServiceReference1.WebServiceTestSoapClient();            /// soap 下面就是要傳過過去使用者名稱和密碼            ServiceReference1.MySoapHeader my = new ServiceReference1.MySoapHeader();            my.Name = "KG";            my.Password = "123456";            /// my 做為一個參數傳過去。           string str=  webclient.HelloWorld(my);           Console.Write(str);           Console.ReadKey();        }    }}
View Code

        (4):結果只有正確輸入的時候才能返回helloword。否則返回的是沒有許可權。

 

.NET中 webservice加密簡談(基礎1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.