• web Service加密处理


    服务端Web Service新建klmywgSoapHeader.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Runtime.InteropServices;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.Services;
    
    using System.Web.Services.Protocols;
    
    /// <summary>
    ///klmywgSoapHeader 的摘要说明
    /// </summary>
    public class klmywgSoapHeader:SoapHeader
    {
        public string UserName;
        public string PassWord;
        private string m_UserName;
        private string m_PassWord;
    
        [DllImport("DataEncrypt.dll")]
        public static extern string DESEncrypt(string Value, string Key);
    
        [DllImport("DataEncrypt.dll")]
        public static extern string DESUnEncrypt(string Value, string Key);
        string key;
    
        public klmywgSoapHeader()
        {
    
            key = ConfigurationManager.AppSettings["key"].ToString();
            m_UserName = ConfigurationManager.AppSettings["WebServiceUsername"].ToString();
            m_PassWord = ConfigurationManager.AppSettings["WebServicePassword"].ToString();
            m_PassWord = DESUnEncrypt(m_PassWord, key);
    
            
        }
        public string Validate()
        {
            PassWord = DESUnEncrypt(PassWord,key);
            if (m_UserName == UserName && m_PassWord == PassWord)
            {
                return "true";
            }
            else
            {
                return "do not have permission";
            }
        }
    
    }

    WebService.cs

    using System;
    
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {
        public klmywgSoapHeader header = new klmywgSoapHeader();
        public Service () {
    
            //如果使用设计的组件,请取消注释以下行 
            //InitializeComponent(); 
        }
    
        [WebMethod]
        [SoapHeader("header")]
        public string HelloWorld() {
           string result= header.Validate();
           if (result == "true")
           {
               return "Hello World";
           }
           else
               return result;
        }
        
    }

    网页调用

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    
    public partial class _Default : System.Web.UI.Page 
    {
        localhost.klmywgSoapHeader header = new localhost.klmywgSoapHeader();
        localhost.Service service = new localhost.Service();    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            header.UserName = ConfigurationSettings.AppSettings["WebServiceUsername"].ToString();
            header.PassWord = ConfigurationSettings.AppSettings["WebServicePassword"].ToString();
    
            service.klmywgSoapHeaderValue = header;
            string result = service.HelloWorld();
            Response.Write(result);
        }
    }
  • 相关阅读:
    Metasploit学习笔记——强大的Meterpreter
    Metasploit学习笔记——社会工程学
    Mono for Andriod学习与实践(1)— 初体验
    Xamarin.Android 在VS下调试时提示 In mgmain JNI_OnLoad 程序“Mono”已退出 解决办法
    Xamarin.Android 使用 SQLiteOpenHelper 进行数据库操作
    WCF 部署时,soap:address location 显示的是电脑名,而不是ip地址
    英特尔® 硬件加速执行管理器安装指南 — Microsoft Windows*
    使用Intel HAXM 加速你的Android模拟器
    Xamarin Studio Android 配置
    Xamarin C# Android for Visual Studio 平台安装
  • 原文地址:https://www.cnblogs.com/engine/p/4262911.html
Copyright © 2020-2023  润新知