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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)
        {

       

     }

}

 

以上是关于ASP.NET Web Service 标准SOAP开发案例代码(自定义验证安全头SOAPHeader)的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET4.0中JavaScript脚本调用Web Service 方法

ASP.NET 访问Web Service 失败

asp.net ajax客户端框架如何调用Web Service

ASP.NET AJAX Call Web Service , Return JSON Format String

在 Azure Service Fabric 中,无状态 Web API 和 ASP.NET Core Web API 之间有啥区别?

初识WebAPI