基本信息
源码名称:WebService通讯 安全认证(基于SoapHeader)源码下载
源码大小:0.06M
文件格式:.zip
开发语言:C#
更新时间:2016-01-20
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


客户端调用代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 客户端调用SoapHeader安全认证测试
{
    class Program
    {
        static void Main(string[] args)
        {
            SoapHeaderExample.ServiceExampleSoapClient she = new SoapHeaderExample.ServiceExampleSoapClient();

            //没有设置SoapHeader的服务调用
            Console.WriteLine("没设置SoapHeader:"   she.HelloWorld(null));
            Console.WriteLine();

            //将用户名与密码存入SoapHeader;
            SoapHeaderExample.MySoapHeader msh = new SoapHeaderExample.MySoapHeader();
            msh.UserName = "dwfir.com";
            msh.PassWord = "dwfir.com";

            //设置SoapHeader的服务调用
            Console.WriteLine("有设置SoapHeader:"   she.HelloWorld(msh));
            Console.Read();
        }
    }
}


webservice端代码:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService基于SoapHeader实现安全认证
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://dwfir.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class ServiceExample : System.Web.Services.WebService
    {
        //声明Soap头实例
        public MySoapHeader MyHeader = new MySoapHeader();

        [WebMethod(Description = "WebService基于SoapHeader实现安全认证《测试》")]
        [SoapHeader("MyHeader")]        
        public string HelloWorld()
        {
            //可以通过存储在数据库中的用户与密码来验证
            string UserName = "dwfir.com", PassWord = "dwfir.com";
            if (MyHeader.UserName.Equals(UserName) & MyHeader.PassWord.Equals(PassWord))
            {
                return "有权限调用此服务,服务调用成功!";
            }
            else
            {
                return "对不起,您没有权限调用此服务!";
            }
        }
    }
}