webService访问加密
具体实现步骤:
1、 定义一个 soapheader派生类用来实现 WebService访问权限验证
- <span style="font-size:10px;">using System;
- using System.Data;
- using System.Configuration;
- 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;
- /// <summary>
- ///MySoapHeader 的摘要说明
- /// </summary>
- public class MySoapHeader:System .Web .Services .Protocols .SoapHeader
- {
- private string _uname = string.Empty;//webservice访问用户名
- public string Uname
- {
- get { return _uname; }
- set { _uname = value; }
- }
- private string _password = string.Empty;//webservice访问密码
- public string Password
- {
- get { return _password; }
- set { _password = value; }
- }
- public MySoapHeader()
- {
- //
- //TODO: 在此处添加构造函数逻辑
- //
- }
- public MySoapHeader(string uname, string upass)
- {
- init(uname, upass);
- }
- private void init(string uname, string upass)
- {
- this._password = upass;
- this._uname = uname;
- }
- //验证用户是否有权访问内部接口
- private bool isValid(string uname, string upass, out string msg)
- {
- msg = "";
- if (uname == "admin" && upass =="admin")
- {
- return true;
- }
- else {
- msg = "对不起!您无权调用此WebService!";
- return false;
- }
- }
- //验证用户是否有权访问外部接口
- public bool isValid(out string msg)
- {
- return isValid(_uname, _password,out msg);
- }
- }
- </span>
2、 定义有需要验证的 WebService。
- <span style="font-size:10px;">using System;
- using System.Collections;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- /// <summary>
- ///test 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
- // [System.Web.Script.Services.ScriptService]
- public class test : System.Web.Services.WebService {
- public test () {
- //如果使用设计的组件,请取消注释以下行
- //InitializeComponent();
- }
- public MySoapHeader myheader = new MySoapHeader();
- [WebMethod]
- public string HelloWorld() {//普通WebService,无需验证
- return "Hello World";
- }
- [SoapHeader("myheader")]//加入此头部的WebService需要验证,不加则为普通WebService无需验证
- [WebMethod(Description = "根据产品编号查询产品的价格", EnableSession = true)]
- public string GetProductPrice2(string ProductId)
- {
- string msg = "";
- //验证是否有权访问
- if (!myheader.isValid(out msg))
- {
- return -1;//返回错误信息
- }
- return ProductId;
- }
- }
- </span>
3、 客户端调用方法
引用 WebService定义 WebService名称为 :Myservice
- <span style="font-size:10px;">using System;
- using System.Configuration;
- using System.Data;
- 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;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- myservice.test te = new abc.test();
- myservice.MySoapHeader myhead = new MySoapHeader();
- myhead.Uname = "admin";//输入WebService访问用户名
- myhead.Password = "admin";//输入WebService访问密码
- te.MySoapHeaderValue = myhead;//
- string test = te.GetProductPrice2("ok!");
- Response.Write(aa);//用户名、密码输入正确则输出ok 否则输出 错误msg
- }
- } </span>
webService访问加密的更多相关文章
- webService访问加密-Soapheader
WebService head加密,可以对 WebService设置访问用户名和密码,增强 WebService的安全性 使 WebService只能被授权用户使用. 具体实现步骤: 1. 定义一个 ...
- WebService安全加密
众所周知,WebService访问API是公开的,知道其URL者均可以研究与调用.那么,在只允许注册用户的WebService应用中,如何确保API访问和通信的安全性呢?本文所指的访问与通信安全性包括 ...
- WebService访问oracle数据库本地调试
WebService访问oracle数据库本地调试-一步一个坑 上篇文章提到我们额数据库挂了,重装了数据库,然后呢我需要在本地调试WebService,看看那些数据结构缺失,迁移到新数据库中去.踩坑之 ...
- 指定WebService访问的语言
场景: 在访问ERP发布的WebService时,由于其指定了访问语言,导致不指定访问语言时,会有部分数据丢失. 解决: 通过WSDL工具生成代理类后,再次对其中的GetWebRequest方法进行重 ...
- webservice访问的几种方式
今天在对接的客户用到了webservice,最终采用wsimport生成本地代理方式以SDK的形式对接,但是想的完整的总结一下ws的几种调用方式. 发布服务的IP地址是:192.168.125.116 ...
- C#访问加密的SQLite数据库
前提:一个项目需要存储各种密码数据,使用的嵌入式的SQLite数据库.默认的SQLite数据库是没有加密的,这样相当不安全.找呀找呀找方法... 方法: 1.使用SQLite管理器加密. 部分SQLi ...
- XE5 ANDROID通过webservice访问操作MSSQL数据库
上接XE5 ANDROID平台 调用 webservice 一.服务端 在ro里添加函数(在impl上添加阿东connection,adoquery,dataprovider) function TN ...
- HTML创建访问加密代码
在</head>前面加入即可 普通方式 此方法屏蔽F12查看源码但是屏蔽不了Ctrl+U查看源码 解决方式加密html即可注意!解密比较繁琐切记要记住自己设置的密码 <SCRIPT ...
- Apache/Nginx的web网页访问加密
随机推荐
- 团队开发——冲刺2.a
冲刺阶段二(第一天) 1.今天准备做什么? 收集游戏图片:开始.暂停.继续.重新开始.退出……为了界面的后期美工做准备. 2.遇到什么困难? 网上的图片很多,但是比较难找到统一风格的.
- 从零开始学习Node.js例子四 多页面实现数学运算 续二(client端和server端)
1.server端 支持数学运算的服务器,服务器的返回结果用json对象表示. math-server.js //通过监听3000端口使其作为Math Wizard的后台程序 var math = r ...
- Scalding初探之二:动手来做做小实验
输入文件 Scalding既可以处理HDFS上的数据,也可以很方便地在本地运行处理一些test case便于debug,Source有好多种 1 TextLine(filename) TextLine ...
- Linux为什么卡住了?
导读 通过SSH登录Linux服务器时,输完用户名就卡住了,要等待10秒钟才提示密码输入.这究竟是什么原因导致的呢? 10秒钟的时间并不算长,吃个薯片喝口咖啡就过去了.但是作为强迫症患者,我还是容不得 ...
- Map/Reduce个人实战--生成数据测试集
背景: 在大数据领域, 由于各方面的原因. 有时需要自己来生成测试数据集, 由于测试数据集较大, 因此采用Map/Reduce的方式去生成. 在这小编(mumuxinfei)结合自身的一些实战经历, ...
- Opencv读取视频
CvCapture 是一个结构体,用来保存图像捕获所需要的信息. opencv提供两种方式从外部捕获图像 一种是从摄像头中, 一种是通过解码视频得到图像. 两种方式都必须从第一帧开始一帧一帧的按顺序获 ...
- TYVJ 1074 武士风度的牛
easy BFS 注意输入是反的就OK了 #include <cstdio> #include <iostream> #include <queue> using ...
- hdu4737 A Bit Fun ——O(n)做法、错误的做法 + 正确做法
囧== 下面的做法是错误的.下午在路上突然明白了== 哎,到现在还是只知道暴力的做法,囧爆了:http://www.cnblogs.com/liuxueyang/p/3322197.html 类似于前 ...
- 2017.1.8a版给信息源新增:max_len、max_db字段
2017.1.8a版程序给信息源增加max_len.max_db字段,分别用于控制:获取条数.数据库保留条数. max_len的说明见此图: max_db的说明见此图: 当max_len和max_db ...
- MFC 线程使用
CWinThread* m_pthread; //多线程对象 struct param//参数传递结构体 { int id; TASK t[20]; }; CNB400Dlg::CNB400Dlg( ...