zoukankan      html  css  js  c++  java
  • 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,否则不能正确显示

    TrackBack:http://www.cnblogs.com/jackyrong/archive/2007/05/23/757708.html

  • 相关阅读:
    寒假Day37:设计模式(封装+继承+多态等)
    INF ClassInstall32 Section详解
    VS2008编译的程序运行提示“由于应用程序配置不正确,应用程序未能启动”
    INF Models Section
    INF DDInstall.Services Section
    INF ClassInstall32.Services Section详解
    INF DDInstall Section
    INF SourceDisksNames Section 和 SourceDisksFiles Section详解
    sys文件查看DbgPrint函数打印的信息
    IRP(I/O Request Package)详解
  • 原文地址:https://www.cnblogs.com/hdjjun/p/1248739.html
Copyright © 2011-2022 走看看