一、 概述:
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兩個類用于控制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 { //這個override的方法會被調(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)容,希望能給大家一個參考,也希望大家多多支持VeVb武林網(wǎng)。