webService访问加密-Soapheader

WebService head加密,可以对 WebService设置访问用户名和密码,增强 WebService的安全性 使 WebService只能被授权用户使用。

具体实现步骤:

1、 定义一个 soapheader派生类用来实现 WebService访问权限验证

[csharp] view plaincopy

  1. <span style="font-size:10px;">using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.HtmlControls;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. /// <summary>
  11. ///MySoapHeader 的摘要说明
  12. /// </summary>
  13. public class MySoapHeader:System .Web .Services .Protocols .SoapHeader
  14. {
  15. private string _uname = string.Empty;//webservice访问用户名
  16. public string Uname
  17. {
  18. get { return _uname; }
  19. set { _uname = value; }
  20. }
  21. private string _password = string.Empty;//webservice访问密码
  22. public string Password
  23. {
  24. get { return _password; }
  25. set { _password = value; }
  26. }
  27. public MySoapHeader()
  28. {
  29. //
  30. //TODO: 在此处添加构造函数逻辑
  31. //
  32. }
  33. public MySoapHeader(string uname, string upass)
  34. {
  35. init(uname, upass);
  36. }
  37. private void init(string uname, string upass)
  38. {
  39. this._password = upass;
  40. this._uname = uname;
  41. }
  42. //验证用户是否有权访问内部接口
  43. private bool isValid(string uname, string upass, out string msg)
  44. {
  45. msg = "";
  46. if (uname == "admin" && upass =="admin")
  47. {
  48. return true;
  49. }
  50. else {
  51. msg = "对不起!您无权调用此WebService!";
  52. return false;
  53. }
  54. }
  55. //验证用户是否有权访问外部接口
  56. public bool isValid(out string msg)
  57. {
  58. return isValid(_uname, _password,out msg);
  59. }
  60. }
  61. </span>

2、 定义有需要验证的 WebService。

[csharp] view plaincopy

  1. <span style="font-size:10px;">using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. using System.Web.Services.Protocols;
  7. using System.Xml.Linq;
  8. /// <summary>
  9. ///test 的摘要说明
  10. /// </summary>
  11. [WebService(Namespace = "http://tempuri.org/")]
  12. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  13. //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
  14. // [System.Web.Script.Services.ScriptService]
  15. public class test : System.Web.Services.WebService {
  16. public test () {
  17. //如果使用设计的组件,请取消注释以下行
  18. //InitializeComponent();
  19. }
  20. public MySoapHeader myheader = new MySoapHeader();
  21. [WebMethod]
  22. public string HelloWorld() {//普通WebService,无需验证
  23. return "Hello World";
  24. }
  25. [SoapHeader("myheader")]//加入此头部的WebService需要验证,不加则为普通WebService无需验证
  26. [WebMethod(Description = "根据产品编号查询产品的价格", EnableSession = true)]
  27. public string GetProductPrice2(string ProductId)
  28. {
  29. string msg = "";
  30. //验证是否有权访问
  31. if (!myheader.isValid(out  msg))
  32. {
  33. return -1;//返回错误信息
  34. }
  35. return ProductId;
  36. }
  37. }
  38. </span>

3、 客户端调用方法

引用 WebService定义 WebService名称为 :Myservice

[csharp] view plaincopy

  1. <span style="font-size:10px;">using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.HtmlControls;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. myservice.test te = new abc.test();
  15. myservice.MySoapHeader myhead = new MySoapHeader();
  16. myhead.Uname = "admin";//输入WebService访问用户名
  17. myhead.Password = "admin";//输入WebService访问密码
  18. te.MySoapHeaderValue = myhead;//
  19. string test = te.GetProductPrice2("ok!");
  20. Response.Write(aa);//用户名、密码输入正确则输出ok 否则输出 错误msg
  21. }
  22. } </span>

也推荐另外一个cookie的方式http://blog.csdn.net/dz45693/article/details/6151170

时间: 2024-08-11 03:38:23

webService访问加密-Soapheader的相关文章

webService访问加密

WebService加密,可以对 WebService设置访问用户名和密码,增强 WebService的安全性 使 WebService只能被授权用户使用. 具体实现步骤: 1. 定义一个 soapheader派生类用来实现 WebService访问权限验证 <span style="font-size:10px;">using System; using System.Data; using System.Configuration; using System.Web;

nginx源码安装、文件模块的修改、访问加密(自定义签名证书)及负载均衡+轮询

主机环境 redhat6.5 实验环境 服务端 ip172.25.29.1    nginx    服务端 ip 172.25.29.2  apache    服务端 ip 172.25.29.3  apache    测试端 ip 172.25.254.29 安装包       nginx-1.10.1.tar.gz nginx用作反向代理 服务端1 1.  安装nginx 1.解压及简单配置 [[email protected] mnt]# yum install gcc -y      #

关于安卓通过webservice访问数据库问题

问题描述: 访问数据库时,手机能增删数据库的数据就是显示不了数据库的里的数据不知道是哪里的问题,用的HTTP这是我webservice中的产看所有信息的方法: public List<string> selectAllCargoInfor() { List<string> list = new List<string>(); try { string sql = "select * from C"; SqlCommand cmd = new SqlC

C#访问加密的SQLite数据库

前提:一个项目需要存储各种密码数据,使用的嵌入式的SQLite数据库.默认的SQLite数据库是没有加密的,这样相当不安全.找呀找呀找方法... 方法: 1.使用SQLite管理器加密. 部分SQLite管理器是有对SQLite数据库有加密功能的.本小菜使用的是:SQLite Developer管理工具.加密如下:        密码就设置OK了. 2.C#访问SQLite带密码的数据库 首先说说,不带密码的SQLite访问字符串格式,只需写入数据库所在路径即可: string ConnStr=

SQLite数据库--C#访问加密的SQLite数据库

前提:一个项目需要存储各种密码数据,使用的嵌入式的SQLite数据库.默认的SQLite数据库是没有加密的,这样相当不安全.找呀找呀找方法... 方法: 1.使用SQLite管理器加密. 部分SQLite管理器是有对SQLite数据库有加密功能的.本小菜使用的是:SQLite Developer管理工具.加密如下:        密码就设置OK了. 2.C#访问SQLite带密码的数据库 首先说说,不带密码的SQLite访问字符串格式,只需写入数据库所在路径即可: string ConnStr=

Delphi调用WebService(通过SoapHeader认证)经验总结

项目(Delphi开发)需要调用另一个系统的WebService.走了不少弯路,现记录总结一下经验.以下是WebService要求: 1.WebService概述 营销Webservice接口采用Apache Axis(version 1.4)技术实现.客户端和服务器用SOAP(Simple Object Access Protocol)协议通过HTTP来交互,客户端根据WSDL描述文档生成SOAP请求消息发送到服务端,服务端解析收到的SOAP请求,调用Web service,然后再生成相应的S

webservice访问的几种方式

今天在对接的时候客户用到了webservice,用wsimport生成本地代理的方式可以访问ws服务,但是想的完整的总结一下ws的几种调用方式. 发布服务的IP地址是:192.168.15.195 客户端访问ws服务的IP是: 1.发布ws服务: 参考:https://www.cnblogs.com/qlqwjy/p/9644078.html applicationContext.xml <?xml version="1.0" encoding="UTF-8"

接口访问加密和限频方案

需求:安全性要求较高的接口暴露到公网中,需要进行加密和限频. 案例:根据用户手机号查询用户ID,需要防止根据phone库非法扫接口,1.返回uid加密:采用RSA加密,私钥加密,公钥解密,为了混淆结果,无论有无uid都返回加密结果.当无结果会根据phone md5截取7位作为伪号,使用另一私钥加密.如果公钥解密失败则说明无对应uid.2.限频,根据key=access_token 50次每天.limit50: timeunit:day public long incr(String key) {

XE5 ANDROID通过webservice访问操作MSSQL数据库

一.服务端 在ro里添加函数(在impl上添加阿东connection,adoquery,dataprovider) function TNewService.getdata(const sqlstr: Utf8String): Variant;beginqry1.Close;qry1.SQL.Text:='';qry1.SQL.Text:=sqlstr;qry1.Open;ds1.Data:=dtstprvdr1.Data;Result:=ds1.XMLData;//传一个xmldata给客户