zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    expdp和impdp用法详解
    Shiro-JWT SpringBoot前后端分离权限认证的一种思路
    Maven Helper 插件-解决依赖冲突
    maven打包之resource配置
    sparkstreaming direct方式读取kafka(0.10版本)数据, 并手动维护offset
    java向kafka发送消息
    idea maven整合log4j
    java设计模式: 工厂方法模式
    异地购房使用武汉公几斤商dai转公几斤dai款
    java设计模式: 单例设计模式
  • 原文地址:https://www.cnblogs.com/engine/p/4262911.html
Copyright © 2011-2022 走看看