华域联盟 .Net ASP.NET中基于soaphead的webservice安全机制

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

使用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安全机制全部内容,希望能给大家一个参考,也希望大家多多支持华域联盟。

您可能感兴趣的文章:

  • ASP.NET使用WebService实现天气预报功能
  • jQuery调用Webservice传递json数组的方法
  • 甩掉ashx和asmx使用jQuery.ajaxWebService请求WebMethod简练处理Ajax
  • 使用jQuery Ajax 请求webservice来实现更简练的Ajax
  • C# WebService发布以及IIS发布
  • jQuery 调用WebService 实例讲解
  • WebService的相关概念

本文由 华域联盟 原创撰写:华域联盟 » ASP.NET中基于soaphead的webservice安全机制

转载请保留出处和原文链接:https://www.cnhackhy.com/67513.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部