• asp.net 2.0中傻瓜式使用soap header


    在websevrice 中,soap header是十分重要的哦,主要是安全性的考虑,在asp.net 2.0中,可以简单地应用soap header来
    进行傻瓜式的应用,更复杂的应用当然要更深入地去看了,

    首先,我们写个简单的helloworld的webservice

    using System;

    using System.Web;

    using System.Web.Services;

    using System.Web.Services.Protocols;

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    public class Service : System.Web.Services.WebService

    {

        public ValidationSoapHeader Authentication;

        private const string DEV_TOKEN = "12345";

        public Service()

        {

            //Uncomment the following line if using designed components

            //InitializeComponent();

        }

        [SoapHeader("Authentication")]

        [WebMethod]

        public string HelloWorld()

        {

            if (Authentication != null && Authentication.DevToken == DEV_TOKEN)

            {

                return "Hello World";

            }

            else

            {

                throw new Exception("Authentication Failed");

            }

        }

    }

    可以看到,首先必须在[webmethod]前添加[SoapHeader("Authentication")],这里我们判断来自客户端的soap header是否有效,并且看其tokern是否等于我们预先定义好的token,如果是的话就输出hello world,否则抛出异常
    这里我们编写一个ValidationSoapHeader类,其中是继承了soap header,如下

    using System;

    using System.Data;

    using System.Configuration;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Web.UI.HtmlControls;

    using System.Web.Services.Protocols;

    /// <summary>

    /// Summary description for ePhoneCredentials

    /// </summary>

    public class ValidationSoapHeader : SoapHeader

    {

        private string _devToken;

        public ValidationSoapHeader()

        {
        }

        public ValidationSoapHeader(string devToken)

        {

            this._devToken = devToken;

        }

        public string DevToken

        {

            get { return this._devToken; }

            set { this._devToken = value; }

        }

    }
    这里只不过是一些属性的设置读取,然后编写一个客户端,如下,显式指定要传递soap header

    //ConsoleMyCsharpClient是建立客户端的工程名
    localhost.ValidationSoapHeader header = new ConsoleMyCsharpClient.localhost.ValidationSoapHeader();

    header.DevToken = "12345";

    localhost.Service ws = new ConsoleMyCsharpClient.localhost.Service();

    ws.ValidationSoapHeaderValue = header;

    Console.WriteLine(ws.HelloWorld());
    Console.ReadLine(); 

    这样,当客户端传递12345给WS后,能正确显示HELLO WORLD,否则不能正确显示

  • 相关阅读:
    PostgreSQL在Update时使用Substring函数截取字符串并且加上CASE WHEN THEN条件判断
    清理Visual Studio 2017的项目历史记录或手工修改Visual Studio 2017的注册表设置
    基于ABP模块组件与依赖注入组件的项目插件开发
    jenkins git can't work ERROR: Timeout after 10 minutes ERROR: Error fetching remote repo 'origin'
    SV randomize
    SV class
    SV coverage
    uvm设计分析——reg
    shell bash-shell
    scrapy的安装
  • 原文地址:https://www.cnblogs.com/mr-lee1976/p/4660163.html
Copyright © 2020-2023  润新知