溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

ASP.NET中Webservice安全如何實(shí)現(xiàn)訪問權(quán)限控制

發(fā)布時(shí)間:2021-05-21 14:37:16 來源:億速云 閱讀:378 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)ASP.NET中Webservice安全如何實(shí)現(xiàn)訪問權(quán)限控制,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

一、 概述:

  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” 特性的簡(jiǎn)單方法

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ù),請(qǐng)取消對(duì)下行的注釋。
  // [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序列化和反序列化的一般過程,可對(duì)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ù),請(qǐng)取消對(duì)下行的注釋。

  // [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";

    }

  }
}

關(guān)于“ASP.NET中Webservice安全如何實(shí)現(xiàn)訪問權(quán)限控制”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI