温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Asp.net中使用soapheader进行验证

发布时间:2021-03-10 14:51:48 来源:亿速云 阅读:170 作者:Leah 栏目:开发技术

如何在Asp.net中使用soapheader进行验证?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

第一步:用来作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进行验证问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI