用SoapHeader保障Web服务的安全

一、建立Web服务,代码如下:
-------------------------------------------------------------------------------------------------------------------------------
1。SoapHeader类:
----------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Web服务的SoapHead类。
/// </summary>
public class ServiceSoapHead : System.Web.Services.Protocols.SoapHeader
{
    /// <summary>
    /// 默认的构造函数。
    /// </summary>
    public ServiceSoapHead()
    {
    }

    /// <summary>
    /// 用户名。
    /// </summary>
    private string userName;
    /// <summary>
    /// 密码。
    /// </summary>
    private string password;

    /// <summary>
    /// 获取或设置用户名。
    /// </summary>
    public string UserName
    {
        get
        {
            return userName;
        }
        set
        {
            userName = value;
        }
    }
    /// <summary>
    /// 获取或设置密码。
    /// </summary>
    public string Password
    {
        get
        {
            return password;
        }
        set
        {
            password = value;
        }
    }

    /// <summary>
    /// 验证用户信息是否合法。如果用户信息不合法则抛出“用户信息验证未通过”的异常。
    /// </summary>
    public void ValidateUserInfo()
    {
        /*
         * 在这里编写实际的用户验证逻辑。
         */
        if (this.userName != "Right" || this.password != "Right")
        {
            throw new Exception("用户信息验证未通过。");
        }
    }
}
-------------------------------------------------------------------------------------------------------------------------------
2。新建Web服务页面Service.asmx,其后台代码如下:
------------------------------------------------------------------------
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    /// <summary>
    /// 默认的构造函数。
    /// </summary>
    public Service ()
    {
    }

    /// <summary>
    /// Web服务的SoapHead类对象。
    /// </summary>
    public ServiceSoapHead MyServiceSoapHead = new ServiceSoapHead();

    /// <summary>
    /// 测试用的方法。
    /// </summary>
    /// <returns>字符串。</returns>
    [WebMethod]
    [SoapHeader("MyServiceSoapHead")]
    public string HelloWorld()
    {
        //在实际业务处理前要先验证用户信息。
        MyServiceSoapHead.ValidateUserInfo();

        return "Hello World";
    }
}
-------------------------------------------------------------------------------------------------------------------------------
二、建立测试网站。相关的页面和代码如下:
-------------------------------------------------------------------------------------------------------------------------------
1。添加上述的Web服务的引用,服务名为“TestService”。
2。建立TestPage.aspx测试页面,其前台的代码和后台代码如下:
----------------------------------------------------------------------------------
前台:
-----------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>测试页面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="在这里显示调用Web服务的结果"></asp:Label><br /><br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="调用Web服务(不提供用户信息)" /><br /><br />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="调用Web服务(提供错误的用户信息)" /><br /><br />
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="调用Web服务(提供正确的用户信息)" /></div>
    </form>
</body>
</html>
----------------------------------------------------------------------------------
后台:
-----------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TestPage : System.Web.UI.Page
{
    /// <summary>
    /// Web服务对象。
    /// </summary>
    private TestService.Service service = new TestService.Service();
    /// <summary>
    /// Web服务的SoapHead类对象。
    /// </summary>
    private TestService.ServiceSoapHead serviceSoapHead = new TestService.ServiceSoapHead();
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.CallWebService();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.serviceSoapHead.UserName = "Error";
        this.serviceSoapHead.Password = "Error";

        this.service.ServiceSoapHeadValue = serviceSoapHead;

        this.CallWebService();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        this.serviceSoapHead.UserName = "Right";
        this.serviceSoapHead.Password = "Right";

        this.service.ServiceSoapHeadValue = serviceSoapHead;

        this.CallWebService();
    }

    /// <summary>
    /// 调用Web服务。
    /// </summary>
    private void CallWebService()
    {
        try
        {
            Label1.Text = this.service.HelloWorld();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
}
-------------------------------------------------------------------------------------------------------------------------------
点击TestPage.aspx测试页面中的各个按钮,看看是不是访问该Web服务有了一定的限制了。:-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值