Asp.net页面中调用soapheader进行验证的操作步骤

Asp.net页面中调用以SOAP头作验证的web services操作步骤

第一步:用来作SOAP验证的类必须从SoapHeader类派生,类中Public的属性将出现在自动产生XML节点中,即:

<soap:Header>
  <UserSoapHeader xmlns="http://tempuri.org/">
   <UserName>string</UserName>
   <Pwd>string</Pwd>
  </UserSoapHeader>
</soap:Header>
 
public class UserSoapHeader : SoapHeader
{
  private string _userName;
  private string _pwd;
  
  //public的属性将自动生成xml结点
  public string UserName
  {
    get { return _userName; }
    set { _userName = value; }
  }
  
  public string Pwd
  {
    get { return _pwd; }
    set { _pwd = value; }
  }
}

第二步:
在WebServices服务类中添加一个public的属性(必须public),类型为从UserSoapHeader

/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
//此属性将作为验证属性
//方法的SoapHeaderAttribute中的名称与此变量一致
  public UserSoapHeader userHeader;
  
  public WebService()
  {
    //如果使用设计的组件,请取消注释以下行
    //InitializeComponent();
  }
  
  [WebMethod]
  [SoapHeader("userHeader")]//这里很重要,名称要和定义的验证属性名称一致!
  public string HelloWorld()
  {
    //进入此方法后,userHeader将自动有值
    if (userHeader != null)
    {
      return "this is retVal : " + userHeader.UserName;
    }
    return " check not successed ";
  }
}

第三步:在客户端进行调用:
1.       添加WEB引用
2.       实例化服务类
3.       实例化SOAP头(在客户端将会自动生成作来作验证的属性;该属性类型为:UserSoapHeader;该属性的名称为:UserSoapHeaderValue) ;自动生成的属性生成规则为:验证类型名称+Value;
4.       调用服务提供的方法。

    WebService s = new WebService();
    UserSoapHeader a = new UserSoapHeader();
    a.UserName = "admin";
    a.Pwd = "zz";
    s.UserSoapHeaderValue = a; //此属性是自动生成的
    Response.Write( s.HelloWorld() ); // this is retVal : admin

 

转:http://www.jb51.net/article/83114.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值