当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET中基于soaphead的webservice安全机制

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

熊出没动画片全集之环球大冒险,吃它让你到80岁都满头黑发,复旦邮箱

使用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安全机制全部内容,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网