程序员

ASP.NET中基于soaphead的webservice安全机制

作者:admin 2021-10-26 我要评论

使用soaphead方法可以在webservice的请求中增加头部信息,当有人调用我们的webservice时,可以通过查询这个请求的头部信息并验证来防止该软件以外的程序调用webser...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

使用soaphead方法可以在webservice的请求中增加头部信息,当有人调用我们的webservice时,可以通过查询这个请求的头部信息并验证来防止该软件以外的程序调用webservice

一、服务端部分

using System;
using System.Web.Services;
using System.Web.Services.Protocols;

//请注意此命名空间必须有别于代理动态连接库上的命名空间。 
//否则,将产生诸如多处定义AuthHeader这样的错误。 
namespace SoapHeadersCS
{

  //由SoapHeader扩展而来的AuthHeader类 
  public class AuthHeaderCS : SoapHeader
  {
    public string Username;
    public string Password;
  }

  //[WebService(Description="用于演示SOAP头文件用法的简单示例")] 
  public class HeaderService
  {

    public AuthHeaderCS sHeader;

    [WebMethod(Description = "此方法要求有调用方自定义设置的soap头文件")]
    [SoapHeader("sHeader")]
    public string SecureMethod()
    {

      if (sHeader == null)
        return "ERROR:你不是VIP用户!";

      string usr = sHeader.Username;
      string pwd = sHeader.Password;

      if (AuthenticateUser(usr, pwd))
      {
        return "成功:" + usr + "," + pwd;
      }
      else
      {
        return "错误:未能通过身份验证";
      }
    }

    private bool AuthenticateUser(string usr, string pwd)
    {

      if ((usr != null) && (pwd != null))
      {
        return true;
      }
      return false;
    }
  }
}

二、客户端部分加上验证的请求

WebService webservice = new WebService();
AuthHeaderCS auth = new AuthHeaderCS();
auth.Username = "vip";
auth.Password = "vippw";
webservice.AuthHeaderCSValue = auth;
textBox1.Text = webservice.SecureMethod();

以上就是基于soaphead的webservice安全机制全部内容,希望能给大家一个参考,也希望大家多多支持站长技术。


原文链接:https://m.jb51.net/article/85406.htm

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • ASP.NET中基于soaphead的webservice安

    ASP.NET中基于soaphead的webservice安

  • 解决antd的Form组件setFieldsValue的警

    解决antd的Form组件setFieldsValue的警

  • TypeScript魔法堂之枚举的超实用手册

    TypeScript魔法堂之枚举的超实用手册

  • node.js如何操作MySQL数据库

    node.js如何操作MySQL数据库