ASP.NET Web Service 標準SOAP開發案例程式碼(自定義驗證安全頭SOAPHeader)

rgqancy發表於2016-02-24

using System.Xml;
using System.Xml.Serialization;
using System.Web.Services.Protocols;
using System.Configuration;
using Service.Common.Constant;

namespace Service.Common.Core.Head.Safe
{
    /// <summary>
    /// 為了安全,自定義的Soap頭
    /// </summary>
    [XmlRoot("Security", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
    public class Security : SoapHeader
    {
        Token _userNameTokern = new Token();

        public Token UsernameToken
        {
            get { return _userNameTokern; }
            set { _userNameTokern = value; }
        }

        /// <summary>
        /// 傳入要正確的使用者名稱和密碼,與訊息頭中儲存的使用者名稱和密碼,進行對比。
        /// </summary>
        /// <param name="rightUserName"></param>
        /// <param name="rightPassWord"></param>
        /// <returns></returns>
        public bool IsAuthorized(string rightUserName, string rightPassWord)
        {
            return this.UsernameToken.Username == rightUserName &&
                this.UsernameToken.Password.Value == rightPassWord;
        }

        /// <summary>
        /// 公共的構造SOAP Header方法
        /// </summary>
        /// <returns></returns>
        public static Security ConstructSafeHead(string username, string passwordvalue, string passwordtype)
        {
            Security soapheader = new Security();
            soapheader.UsernameToken = new Token();
            soapheader.UsernameToken.Username = username;
            soapheader.UsernameToken.Password = new Password();
            soapheader.UsernameToken.Password.Value = passwordvalue;
            soapheader.UsernameToken.Password.Type = passwordtype;

            return soapheader;
        }
    }
}

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using Service.ESB.BaseData.Operation;
using Service.Common.Core.ESB.Request;
using Service.Common.Core.Head.Safe;
using Service.Common.Core.ESB.Response;
using Service.Common.Core.Base;
using Service.ESB.BaseData.Simulation;
using Service.Common.Log;
using Service.Common.Core.Head.Test;
using System.Xml;
using System.Xml.Serialization;
using Service.Common;
using Service.Common.Core.ESB.Base;
using Service.Common.Constant;
using Service.ESB.BaseData.Simulation.Operation;

namespace Service.ESB.BaseData
{
    /// <summary>
    /// 模擬ESB匯流排資料通用服務的類
    /// </summary>
    [WebService(Namespace = "http://www.iec.ch/TC57/2008/schema/message")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class SimulativeDataCommonService
    {
        #region 私有欄位
        /// <summary>
        /// 安全頭
        /// </summary>
        private Security _safeSoapHeader;

        /// <summary>
        /// 測試標識頭
        /// </summary>
        private TestCaseHead _testCaseHeader;
        #endregion

        #region 公開的屬性
        [XmlElement(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Security SafeSoapHeader
        {
            get { return _safeSoapHeader; }
            set { _safeSoapHeader = value; }
        }

        public TestCaseHead TestCaseHeader
        {
            get { return _testCaseHeader; }
            set { _testCaseHeader = value; }
        }
        #endregion

        #region 服務方法
        [SoapHeader("SafeSoapHeader")]//安全頭
        [SoapHeader("TestCaseHeader")]//測試標識頭--用於測試時標示
        [SoapDocumentMethod(RequestElementName = "RequestMessage", ResponseElementName = "ResponseMessage")]
        [WebMethod(Description = "模擬資料通用服務的介面")]
        [return: XmlElement("RequestMessage"), XmlElement("Header", typeof(HeaderClass)), XmlElement("Reply", typeof(ReplyClass))]
        [XmlInclude(typeof(RequestMessage)), XmlInclude(typeof(ResponseMessage))]
        public object[] invokeDataService(HeaderClass reqMsgHeader, RequestClass reqMsgRequest)
        {

       

     }

}

 

相關文章