WebService基于SoapHeader实现安全认证(二)

支持通过Http请求方法调用webservice,同时支持SoapHeader验证。

using Globalegrow.Common;
using Globalegrow.Model;
using System.Web;
using System.Web.Services.Protocols;

namespace Globalegrow.Service
{
    /// <summary>
    /// 自定义SoapHeader类。
    /// </summary>
    public class Certificate : SoapHeader
    {
        private static ServiceConfig M_ServiceConfig;

        /// <summary>
        /// 构造函数
        /// </summary>
        public Certificate()
        {
            //=>用于处理Http请求方式调用WebService服务所需要验证的SoapHeader信息。
            this.UserName = HttpContext.Current.Request["UserName"];
            this.PassWord = HttpContext.Current.Request["PassWord"];
        }

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static Certificate()
        {
            //=>创建Certificate类的实例时,为AppContext.ServiceConfig 赋值
            M_ServiceConfig = ServiceConfigUtility.Load();
            AppContext.ServiceConfig = M_ServiceConfig;
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        public string PassWord { get; set; }

        /// <summary>
        /// 用户验证
        /// </summary>
        public bool IsVaild(out string message)
        {
            string _UserName = M_ServiceConfig.WebServiceTokenAccount;
            string _PassWord = M_ServiceConfig.WebServiceTokenPassword;
            if (this.UserName == _UserName && this.PassWord == _PassWord)
            {
                message = "";
                return true;
            }
            else
            {
                message = "对不起,您没有权限调用此服务!";
                return false;
            }
        }
    }
}

 调用:

http://192.168.3.41:8888/PermissionService/PermissionService.asmx/GetAuthUserByRoleID?UserName=admin&PassWord=admin&roleid=11

 

posted @ 2015-02-06 10:46  跟着阿笨一起玩.NET  阅读(1078)  评论(0编辑  收藏  举报