一般在项目中,制作的都是基于SOAP协议的webservices,其描述语言是WSDL。但是有时候在项目中,需要保证webservices的安全,需要对其进行进行验证,那么我们就要实现SoapHeader,具体的实现方式如下:

首先就是自定义一个类,继承自System.Web.Services.Protocols.SoapHeader ,然后在这个类中,通过暴露的公共属性和方法来进行校验。

其次就是在webservices主体方法中,添加对SoapHeader的验证的支持

最后直接在用户页面进行验证,调用方法即可。

首先,定义一个sSoapHeader类,暴露出其提供的公共验证字段,并提供验证方法:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace webServiceDemo
{
    public class sSoapHeader:System.Web.Services.Protocols.SoapHeader
    {
        private string _UserId = string.Empty;
        private string _UserPwd = string.Empty;         public sSoapHeader() { }         public string UserId
        {
            get
            {
                return _UserId;
            }
            set
            {
                _UserId = value;
            }
        }         public string UserPwd
        {
            get
            {
                return _UserPwd;
            }
            set
            {
                _UserPwd = value;
            }
        }         public void Initial(string username, string password)
        {
            UserId = username;
            UserPwd = password;
        }         public bool IsValid(string uid, string pwd, out string msg)
        {
            msg = "";
            if (uid == "admin" && pwd == "admin888")
            {
                return true;
            }
            else
            {
                msg = "对不起,你无法调用";
                return false;
            }
        }
        public bool IsValid(out string msg)
        {
            return IsValid(_UserId, _UserPwd, out msg);
        }     }
}

其次就是在webservice主体中,添加对此验证方法的支持


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols; namespace webServiceDemo
{
    /// <summary>
    /// Service1 的摘要说明
    /// </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 Service1 : System.Web.Services.WebService
    {
        public sSoapHeader s = new sSoapHeader();
        [SoapHeader("s")]   //这里就是添加验证方法的标志
        [WebMethod]
        public string HelloWorld(int a,int b)
        {
            string msg = "";
            if (!s.IsValid(out msg))
            {
                return msg;
            }
            return (a + b).ToString();
        }        
    }
}

在上图中,我已经标志的很清楚了[SoapHeader("s")] 就是添加验证方法的标志。

最后就是在用户页面中进行调用,引用完毕后,需要对soapheader暴露的公共属性方法进行赋值判断,最后调用即可。只要验证正确,便可以正常调用,否则则提示没有权限:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WEB
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            localhost.Service1 s = new WEB.localhost.Service1();
            localhost.sSoapHeader header = new WEB.localhost.sSoapHeader();
            header.UserId = "admin";
            header.UserPwd = "admin888";
            s.sSoapHeaderValue = header;
            Response.Write(s.HelloWorld(2,1));
        }
    }
}  
 
 
 

带SoapHeader验证的WebServices的更多相关文章

  1. C#动态调用带有SoapHeader验证的WebServices

    http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...

  2. C#静态调用带有SoapHeader验证的WebServices

    转自:http://blog.csdn.net/u012995964/article/details/54562111 本文记录带有SoapHeader验证的WebServices服务创建.部署及C# ...

  3. ANDROID调用webservice带soapheader验证

    最近的一个项目中调用webservice接口,需要验证soapheader,现将解决方法记录如下:(网上资料出处太多,就不做引用,原作者如看到,如有必要添加请通知) 1.先看接口 POST /webs ...

  4. .net 在不同情况下调用带soapheader的webservice的方式

    国庆长假到了,本想出去玩玩,无奈自己屌丝一枚,啥都没有,只能自己宅在家里思考思考人生.不过人生还是过于复杂,一时间也想不出个所以然,只能是整理一下在工作中遇到的一些小问题,首先是关于带soaphead ...

  5. C#访问Java的WebService添加SOAPHeader验证的问题

    原文:C#访问Java的WebService添加SOAPHeader验证的问题 这两天做与公司OA的接口,发现C#访问Java的WebService需要提供一个SOAP的头验证信息,但是WebServ ...

  6. .net SoapHeader验证

    .net SoapHeader验证 在工作中经常用到webservice,在.net 开发中经常用到webservice,在java开发经常用到cxf. 今天闲置没事就介绍下 .net webserv ...

  7. MongoDB系列:四、spring整合mongodb,带用户验证

    在前面的两篇博客 MongoDB常用操作练习.springboot整合mongoDB的简单demo中,我们基本上熟悉了mongodb,也把它与spring boot进行了整合并且简单使用.在本篇博客中 ...

  8. C#调用Java的WebService添加SOAPHeader验证(2)

    C#调用Java的WebService添加SOAPHeader验证 上一篇链接如上,更像是 Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) 来处理xml, ...

  9. C#调用Java的WebService添加SOAPHeader验证

    C#调用Java的WebService添加SOAPHeader验证(2) 1.问题描述 调用的Java的webservice string Invoke(string func, string req ...

随机推荐

  1. linux下网卡bonding配置

    linux下网卡bonding配置   章节 bonding技术 centos7配置bonding centos6配置bonding 一.bonding技术 bonding(绑定)是一种linux系统 ...

  2. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  3. spring AOP pointcut expression表达式解析

    Pointcut 是指那些方法需要被执行"AOP",是由"Pointcut Expression"来描述的.Pointcut可以有下列方式来定义或者通过& ...

  4. 基于markdown的blog系统调研1:typecho

    ))

  5. Python读取word文档(python-docx包)

    最近想统计word文档中的一些信息,人工统计的话...三天三夜吧 python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.read ...

  6. [转]postman 官方文档解说

    1. 安装 两种安装方式,我热衷于以chrome插件形式安装 Chrome插件 Mac App 2. 发送请求 Postman最基础的功能就是发送http请求,支持GET/PUT/POST/DELET ...

  7. Live555 中的客户端动态库.so的调用方式之一 程序中调用

    1.  打开动态链接库:    #include <dlfcn.h>    void *dlopen(const char *filename, int flag);    该函数返回操作 ...

  8. 怎样去掉a标签的蓝框

    直接上代码 *{-webkit-tap-highlight-color:rgba(255,0,0,0);} 我是直接给每一个都加了这个属性   其实没有必要 因为 只有a 标签 input 标签 和t ...

  9. 利用solr6.5,tomcat9.0和centos7.0的搭建

    第一步:去官网下载所需的软件包, jdk1.8   wget http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff ...

  10. iOS ARC也会有内存泄露

    本文转载至 http://blog.csdn.net/allison162004/article/details/38753219  iOS提供了ARC功能,很大程度上简化了内存管理的代码. 但使用A ...