惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
Last Week in AI
Last Week in AI
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
IT之家
IT之家
Google DeepMind News
Google DeepMind News
D
Docker
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
月光博客
月光博客
宝玉的分享
宝玉的分享
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 广交天下好友

给 IIS 配置 HTTPS maui发布安卓系统 MAUI 运行环境注意事项 MQTT项目应用 MQTT 网络方面小知识 电脑小知识 Redis操作篇 Modbus 软件安装注册码 wpf 关闭热重载 重装vs2022 nuget添加包报错: Unexpected character encountered while parsing value: �. Path '', line 0, position 0. WPF 踩过的坑 vs2019 未能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。 sql 备忘录 Excel 其它操作 护眼颜色调整 练习第一篇 Python Requests使用 第一篇 C#模拟http请求抓取数据 好文记录地址 关于邮件发送和邮件附件接收方面 sql 查询时间当前时间少7天 20190729研究和学习篇 C# Timer同步和异步使用 马肯9450命令回传 上海公积金社保业务办理
asp.net webService添加头文件验证
广交天下好友 · 2020-03-06 · via 博客园 - 广交天下好友

第一步 ,新建asp.net 项目

第二步 ,添加MyTest ws服务

代码如下

2.1 建立头文件需要验证的实体类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Services.Protocols;
 6 
 7 namespace Aspheadnet.Models
 8 {
 9     public class UserSoapHeader : SoapHeader
10     {
11         private string _userName;
12 
13         private string _pwd;
14 
15         //public的属性将自动生成xml结点
16 
17         public string UserName
18         {
19 
20             get { return _userName; }
21 
22             set { _userName = value; }
23 
24         }
25         public string Pwd
26         {
27 
28             get { return _pwd; }
29 
30             set { _pwd = value; }
31 
32         }
33     }
34 }

2.2 编写ws服务

 1 using Aspheadnet.Models;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.Web.Services;
 7 using System.Web.Services.Protocols;
 8 
 9 namespace Aspheadnet
10 {
11     /// <summary>
12     /// MyTest 的摘要说明
13     /// </summary>
14     [WebService(Namespace = "http://tempuri.org/")]
15     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
16     [System.ComponentModel.ToolboxItem(false)]
17     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
18     // [System.Web.Script.Services.ScriptService]
19     public class MyTest : System.Web.Services.WebService
20     {
21 
22         //此属性将作为验证属性
23         //方法的SoapHeaderAttribute中的名称与此变量一致
24         public UserSoapHeader userHeader;
25 
26         [WebMethod]
27         [SoapHeader("userHeader")]//这里很重要,名称要和定义的验证属性名称一致!
28         public string HelloWorld()
29         {
30             //进入此方法后,userHeader将自动有值
31             if (userHeader != null)
32             {
33                 if (userHeader.UserName != "张三")
34                 {
35                     return "用户名不匹配";
36                 }
37                 if (userHeader.Pwd != "123")
38                 {
39                     return "密码不匹配";
40                 }
41 
42                 return "请求成功";
43 
44             }
45 
46             return "无效请求";
47         }
48     }
49 }

第3步客户端调用

3.1 添加webservice服务引用

1     <client>
2       <endpoint address="http://192.168.21.195:8033/MyTest.asmx" binding="basicHttpBinding"
3         bindingConfiguration="MyTestSoap" contract="TestService.MyTestSoap"
4         name="MyTestSoap" />
5     </client>

这里我发布了本地测试iis环境

3.2 界面写入相关代码

        protected void Button1_Click(object sender, EventArgs e)
        {
            test();
        }

        void test()
        {
            client.TestService.MyTestSoapClient mc = new MyTestSoapClient();

            WebService s = new WebService();
            UserSoapHeader a = new UserSoapHeader();

            a.UserName = "张三";

            a.Pwd = "123";

            Response.Write(mc.HelloWorld(a)); 
        }

效果如下

请求成功说明过了头文件认证了。

  3.2 如果头文件不参入参数,或者参数传入有误都会无效请求。

下面这篇文件介绍怎么抓取这个页面的按钮事件。

https://www.cnblogs.com/suntanyong88/p/12426758.html