当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net页面中调用soapheader进行验证的操作步骤

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

2017年12月12日  | 移动技术网IT编程  | 我要评论
本文为大家分享了asp.net页面中调用以soap头作验证的web services操作步骤,供大家参考,具体内容如下 第一步:用来作soap验证的类必须从soaphea

本文为大家分享了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
 

很简单吧,希望大家都能够掌握asp.net中用soapheader作验证的步骤,谢谢大家的阅读。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网