加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webService访问加密-Soapheader

发布时间:2020-12-16 22:30:24 所属栏目:安全 来源:网络整理
导读:WebService head 加密,可以对 ?WebService 设置访问用户名和密码,增强 ?WebService 的安全性 ? 使 ?WebService 只能被授权用户使用。 ? 具体实现步骤: ? 1、? 定义一个 ?soapheader 派生类用来实现 ?WebService 访问权限验证 [csharp] ? view plain copy
WebService head加密,可以对?WebService设置访问用户名和密码,增强?WebService的安全性?使?WebService只能被授权用户使用。?

具体实现步骤:?

1、?定义一个?soapheader派生类用来实现?WebService访问权限验证
[csharp]? view plain copy
  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. ??
  11. ///?<summary>??
  12. ///MySoapHeader?的摘要说明??
  13. ///?</summary>??
  14. public?class?MySoapHeader:System?.Web?.Services?.Protocols?.SoapHeader???
  15. {??
  16. ????private?string?_uname?=?string.Empty;//webservice访问用户名??
  17. ??
  18. string?Uname??
  19. ????{??
  20. ????????get?{?return?_uname;?}??
  21. ????????set?{?_uname?=?value;?}??
  22. ????}??
  23. ????string?_password?=?//webservice访问密码??
  24. string?Password??
  25. ????{??
  26. return?_password;?}??
  27. set?{?_password?=?value;?}??
  28. ????}??
  29. public?MySoapHeader()??
  30. ?????//??
  31. ?????//TODO:?在此处添加构造函数逻辑??
  32. public?MySoapHeader(string?uname,?string?upass)??
  33. ????????init(uname,?upass);??
  34. void?init(this._password?=?upass;??
  35. this._uname?=?uname;??
  36. ????//验证用户是否有权访问内部接口??
  37. bool?isValid(string?upass,153); font-weight:bold; background-color:inherit">out?string?msg)??
  38. ????????msg?=?"";??
  39. if?(uname?==?"admin"?&&?upass?=="admin")??
  40. ????????{??
  41. ????????????return?true;??
  42. ????????}??
  43. else?{??
  44. ????????????msg?=?"对不起!您无权调用此WebService!";??
  45. false;??
  46. ????//验证用户是否有权访问外部接口??
  47. string?msg)??
  48. return?isValid(_uname,?_password,out?msg);??
  49. }??
  50. </span>??


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

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



3、?客户端调用方法?

引用?WebService定义?WebService名称为?:Myservice

copy
    using?System.Configuration;??
  1. using?System.Data;??
  2. public?partial?class?_Default?:?System.Web.UI.Page???
  3. protected?void?Page_Load(object?sender,?EventArgs?e)??
  4. ????????myservice.test?te?=?new?abc.test();??
  5. ????????myservice.MySoapHeader?myhead?=? ????????myhead.Uname?=?"admin";//输入WebService访问用户名??
  6. ????????myhead.Password?=?"admin";//输入WebService访问密码??
  7. ????????te.MySoapHeaderValue?=?myhead;string?test?=?te.GetProductPrice2("ok!");??
  8. ??????Response.Write(aa);//用户名、密码输入正确则输出ok?否则输出?错误msg??
  9. }?</span>??

?

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


转载:http://www.voidcn.com/article/p-bftoczfm-vv.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读