ASP.NET Web Service 标准SOAP开发案例代码(自定义验证安全头SOAPHeader)

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)
        {

       

     }

}

 

posted @ 2016-02-24 18:06  任国强  阅读(1123)  评论(0编辑  收藏  举报