通过SoapHeader来增强Web Service的安全性
1.Web Service实现步骤
首先引入命名空间
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols; //对soap Head引用添加的命名空间
(1)定义自己的SoapHeader派生类。
public class MyHeader : System.Web.Services.Protocols.SoapHeader
{
private string _UserID = string.Empty;
private string _PassWord = string.Empty;
///
/// 构造函数
///
public MyHeader()
{
}
///
/// 构造函数
///
/// 用户ID
/// 加密后的密码
public MyHeader(string nUserID, string nPassWord)
{
Initial(nUserID, nPassWord);
}
#region 属性
///
/// 用户名
///
public string UserID
{
get { return _UserID; }
set { _UserID = value; }
}
///
/// 加密后的密码
///
public string PassWord
{
get { return _PassWord; }
set { _PassWord = value; }
}
#endregion
#region 方法
///
/// 初始化
///
/// 用户ID
/// 加密后的密码
public void Initial(string nUserID, string nPassWord)
{
UserID = nUserID;
PassWord = nPassWord;
}
///
/// 用户名密码是否正确
///
/// 用户ID
/// 加密后的密码
/// 返回的错误信息
///
public bool IsValid(string nUserID, string nPassWord, out string nMsg)
{
nMsg = "";
try
{
//判断用户名密码是否正确
if (nUserID == "admin" && nPassWord == "admin")
{
return true;
}
else
{
nMsg = "对不起,你无权调用此Web服务,可能有如下原因:/n 1.您的帐号被管理员禁用。/n 2.您的帐号密码不正确";
return false;
}
}
catch
{
nMsg = "对不起,你无权调用此Web服务,可能有如下原因:/n 1.您的帐号被管理员禁用。/n 2.您的帐号密码不正确";
return false;
}
}
///
/// 用户名密码是否正确
///
///
public bool IsValid(out string nMsg)
{
return IsValid(_UserID, _PassWord, out nMsg);
}
#endregion
}
(2)添加基于SoapHeader验证的Web Service接口方法:
///
/// myService 的摘要说明
///
//[WebService(Namespace="http://MyServer/MyWebServices/")]
public class myService : System.Web.Services.WebService
{
///
/// Soap头实例
///
public MyHeader myHeader = new MyHeader(); //实例化SoapHead
public myService()
{
//CodeGenerated: 自动生成的代码,该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
}
///
/// 清理所有正在使用的资源
///
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// WEB 服务示例
// HelloWorld() 示例服务返回字符串 Hello World
// 若要生成,请取消注释下列行,然后保存并生成项目
// 若要测试此 Web 服务,请按 F5 键
///
/// 正常调用的方法
///
///
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
///
/// 需要验证的方法,添加SoapHeader标识
///
///
///
[SoapHeader("myHeader")]
[WebMethod(Description = "HelloWord", EnableSession = true)]
public string HelloWorld2(string contents)
{
string msg = "";
//验证是否有权访问
if (!myHeader.IsValid(out msg))
return msg;
return "Hello World:" + contents;
}
}
2.客户端调用具有SoapHeader的Web Service
//创建myService对象
StudySoapHead.myService service = new StudySoapHead.myService();
//创建soap头对象
StudySoapHead.MyHeader header=new StudySoapHead.MyHeader();
//设置soap头变量
header.PassWord = "admin";
header.UserID = "admin";
service.myHeader = header;
//调用web 方法
Console.WriteLine(service.HelloWorld());
Console.WriteLine(service.HelloWorld2("Valid request!"));
Console.Read();
通过SoapHeader对用户口令进行验证,只有授权的用户才可以使用该接口。确保了访问接口用户的安全性。
转自:http://blog.csdn.net/dongxinxi/article/details/6305656#comments
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/22392018/viewspace-766626/ ,如需转载,请注明出处,否则将追究法律责任。