一、 概述:
Web Services是由企業發布的完成其特定商務需求的線上應用服務,其他公司或應用軟體能夠透過Internet來存取並使用這項線上服務。它邏輯性的為 其他應用程式提供資料與服務.各應用程式透過網路協定與規定的一些標準資料格式(Http,XML,Soap)來存取Web Service,透過Web Service內部執行得到所需結果。由於它透過internet進行呼叫,必然存在網路使用者都可以呼叫的安全性問題。如何實作webservice的存取 權限限制,是使用webservice使用者使用面臨重要的問題,下文就給兩個方案,從淺到深解決上面問題。
二、基於「soapheader」 特性的簡單方法
1." soapheader" 概述
SOAP 標頭提供了一種方法,用於將資料傳遞到XML Web services 方法或從XML Web services 方法傳遞資料,條件是該資料不直接與XML Web services 方法的主功能相關。 多數情況下用來傳遞使用者身分驗證資訊,當然它的作用遠不止如此,有待於在實際應用中發掘。
2.soapheader實作使用者驗證碼
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 从脚本中调用此 Web 服务,请取消对下行的注释。 // [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.缺點分析:
(1)服務邏輯與使用者權限驗證邏輯混和,加大程式理解複雜度。
(2)權限邏輯重用性不高
二、基於“SoapExtensionAttribute” 特性的方法
1.SoapExtensionAttribute與SoapExiosion概述
SoapExtension和So Attribute兩個類別用於控制webservice序列化和反序列化的一般過程,可對webservice進行壓縮和日誌等功能進行控制.
2.實現代碼
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 { //这个override的方法会被调用四次 //分别是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("认证失败", 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 从脚本中调用此 Web 服务,请取消对下行的注释。 // [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() { //业务逻辑. return "Something done"; } } }
以上就是Webservice的安全設定全部內容,希望能給大家一個參考,也希望大家多多支援PHP中文網。
更多ASP.NET中Webservice安全 實現存取權限控制相關文章請關注PHP中文網!

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。

要開始C#.NET開發,你需要:1.了解C#的基礎知識和.NET框架的核心概念;2.掌握變量、數據類型、控制結構、函數和類的基本概念;3.學習C#的高級特性,如LINQ和異步編程;4.熟悉常見錯誤的調試技巧和性能優化方法。通過這些步驟,你可以逐步深入C#.NET的世界,並編寫高效的應用程序。

C#和.NET的關係是密不可分的,但它們不是一回事。 C#是一門編程語言,而.NET是一個開發平台。 C#用於編寫代碼,編譯成.NET的中間語言(IL),由.NET運行時(CLR)執行。

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中