加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在webservice中简单使用SoapHeader

发布时间:2020-12-17 00:32:57 所属栏目:安全 来源:网络整理
导读:环境:visual studio 2005 --ASP.net 在我的例子中,主要是简单的定义一个SoapHeader,并且在客户端和Webservice中使用。 场景描述: ?????? 用户在文本框中输入用户名和密码,点击提交按钮去调用一个webservice的方法来处理。在客户端发送请求的时候,用户

环境:visual studio 2005 --ASP.net

在我的例子中,主要是简单的定义一个SoapHeader,并且在客户端和Webservice中使用。

场景描述:

?????? 用户在文本框中输入用户名和密码,点击提交按钮去调用一个webservice的方法来处理。在客户端发送请求的时候,用户名和密码的信息保存在soapheader中。webservice方法进行处理的时候,通过从soapheader中取出用户名和方法进行处理。

?

1、SoapHeader类的定义

?? using System.Web.Services.Protocols;

?

?? class MySoapHeader:SoapHeader

?? {

????????? public string userName;

????????? public string password;

?? }

?

2.webservice 中使用soapheader

定义一个webservice,命名为FirstService.asmx(使用自动生成的就可以了,修改一下HelloWord方法)

?

public class FirstService : System.Web.Services.WebService {?

??? public FirstService () {

??????? //如果使用设计的组件,请取消注释以下行
??????? //InitializeComponent();
??? }

?

?? //声明一个MySoapHeader
??? public MySoapHeader mySoapHeader;

?

??? [WebMethod]
??? [SoapHeader("mySoapHeader")]
??? public string HelloWorld() {
??????? if (mySoapHeader.userName == "vicky")
??????? {
??????????? return "ok";
??????? }
??????? return "error";
??? }

?

3.客户端使用soapheader

??? 提交按钮的单击事件

?

protected void Button1_Click(object sender,EventArgs e)
??? {????????

?????? //new 一个webservice的代理类

??????? FirstService service = new FirstService();
??????? service.mySoapHeader.userName = userName.Text;

??????? service.mySoapheader.password = password.Text;


??????? Label3.Text = service.HelloWorld();??????????
??? }

?

?ok,完成,可以发现当点击提交按钮时,用户名和密码是放在soap消息头中的。

?客户端发送的消息如下:

POST /WebSite1/FirstService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/HelloWorld"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns="http://tempuri.org/">
      <userName>string</userName>
      <password>string</password>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <HelloWorld xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>
这是最基本的使用,如果想增加webservice消息的安全,可以利用soap扩展,对soap消息中的元素进行安全处理。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读