ASP.NET中Webservice安全 實(shí)現(xiàn)訪問權(quán)限控制
來源:易賢網(wǎng) 閱讀:1959 次 日期:2016-08-08 14:36:30
溫馨提示:易賢網(wǎng)小編為您整理了“ASP.NET中Webservice安全 實(shí)現(xiàn)訪問權(quán)限控制”,方便廣大網(wǎng)友查閱!

本文主要講解ASP.NET中的Webservice的安全設(shè)置兩種方法,一種基于soapheader,一種基于SoapExtensionAttribute,需要的朋友可以參考下。

一、 概述:

Web Services是由企業(yè)發(fā)布的完成其特定商務(wù)需求的在線應(yīng)用服務(wù),其他公司或應(yīng)用軟件能夠通過Internet來訪問并使用這項(xiàng)在線服務(wù)。它邏輯性的為 其他應(yīng)用程序提供數(shù)據(jù)與服務(wù).各應(yīng)用程序通過網(wǎng)絡(luò)協(xié)議和規(guī)定的一些標(biāo)準(zhǔn)數(shù)據(jù)格式(Http,XML,Soap)來訪問Web Service,通過Web Service內(nèi)部執(zhí)行得到所需結(jié)果。由于它通過internet進(jìn)行調(diào)用,必然存在網(wǎng)絡(luò)用戶都可以調(diào)用的安全問題。如何實(shí)現(xiàn)webservice的訪問 權(quán)限限制,是使用webservice用戶使用面臨重要的問題,下文就給兩種方案,從淺到深解決上面問題。

二、基于“soapheader” 特性的簡單方法

1." soapheader" 概述  

SOAP 標(biāo)頭提供了一種方法,用于將數(shù)據(jù)傳遞到 XML Web services 方法或從 XML Web services 方法傳遞數(shù)據(jù),條件是該數(shù)據(jù)不直接與 XML Web services 方法的主功能相關(guān)。 多數(shù)情況下用來傳遞用戶身份驗(yàn)證信息,當(dāng)然它的作用遠(yuǎn)不止如此,有待于在實(shí)際應(yīng)用中發(fā)掘。

2.soapheader實(shí)現(xiàn)用戶身份驗(yàn)證代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

namespace UserCenter

{

  public class MySoapHeader :SoapHeader

  {

    public string UserName

    {

      get;

      set;

    }

    public string PWD

    {

      get;

      set;

    }

  }

  /// <summary>

  /// MyMath 的摘要說明

  /// </summary>

  [WebService(Namespace = "http://tempuri.org/")]

  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

  [System.ComponentModel.ToolboxItem(false)]

  // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。

  // [System.Web.Script.Services.ScriptService]

  public class MyMath : System.Web.Services.WebService

  {

    public MySoapHeader sHeader;

    [WebMethod]

    public string HelloWorld()

    {

      return "Hello World";

    }

    [WebMethod]

    [SoapHeader("sHeader")]

    public string add(int x, int y)

    {

      if (sHeader.UserName == "test" && sHeader.PWD == "test")

      {

        return (x + y).ToString();

      }

      else

      {

        return null;

      }

    }

  }

}

3.缺點(diǎn)分析:

(1)服務(wù)邏輯和用戶權(quán)限驗(yàn)證邏輯混和,加大程序理解復(fù)雜度。

(2)權(quán)限邏輯重用性不高

二、基于“SoapExtensionAttribute” 特性的方法

1.SoapExtensionAttribute與SoapExtension概述

SoapExtension和SoapExtensio。Attribute兩個(gè)類用于控制webservice序列化和反序列化的一般過程,可對webservice進(jìn)行壓縮和日志等功能進(jìn)行控制.

2.實(shí)現(xiàn)代碼 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

namespace XMLClass1.class15.content

{

  [AttributeUsage(AttributeTargets.Method)]

  public class MyExtensionAttribute : SoapExtensionAttribute

  {

    int _priority = 1;

    public override int Priority

    {

      get { return _priority; }

      set { _priority = value; }

    }

    public override Type ExtensionType

    {

      get { return typeof(MyExtension); }

    }

  }

  public class MyExtension : SoapExtension

  {

    //這個(gè)override的方法會(huì)被調(diào)用四次

    //分別是SoapMessageStage BeforeSerialize,AfterSerialize,BeforeDeserialize,AfterDeserialize

    public override void ProcessMessage(SoapMessage message)

    {

      if (message.Stage == SoapMessageStage.AfterDeserialize)//反序列化之后處理

      {

        bool check = false;

        foreach (SoapHeader header in message.Headers)

        {

          if (header is MySoapHeader)

          {

            MySoapHeader myHeader = (MySoapHeader)header;

            if (myHeader.Name == "admin" || myHeader.PassWord == "admin")

            {

              check = true;

              break;

            }

          }

        }

        if (!check)

          throw new SoapHeaderException("認(rèn)證失敗", SoapException.ClientFaultCode);

      }

    }

    public override Object GetInitializer(Type type)

    {

      return GetType();

       }

    public override Object GetInitializer(LogicalMethodInfo info, SoapExtensionAttribute attribute)

    {

      return null;

    }

    public override void Initialize(Object initializer)

    {

    }

  }

  public class MySoapHeader : SoapHeader

  {

    string _name;

    string _passWord;

    public string Name

    {

      get { return _name; }

      set { _name = value; }

    }

    public string PassWord

    {

      get { return _passWord; }

      set { _passWord = value; }

    }

  }

  /// <summary>

  /// headersoap2 的摘要說明

  /// </summary>

  [WebService(Namespace = http://tempuri.org/)]

  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

  [System.ComponentModel.ToolboxItem(false)]

  // 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。

  // [System.Web.Script.Services.ScriptService]

  public class headersoap2 : System.Web.Services.WebService

  {

     public MySoapHeader header;

    [WebMethod]

    [MyExtensionAttribute]

    [SoapHeader("header", Direction = SoapHeaderDirection.In)]

    public string CheckHeader()

    {

      //業(yè)務(wù)邏輯.

      return "Something done";

    }

  }

}

以上就是Webservice的安全設(shè)置全部內(nèi)容,希望能給大家一個(gè)參考

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:ASP.NET中Webservice安全 實(shí)現(xiàn)訪問權(quán)限控制
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)