.NET中 webservice加密简谈(基础1)

   在博客园都呆了几年了,一直都是看各位大神的文章。自己从来也没亲自写多点点。本码农打算从今年去也开始写写文章,打打基础,以备不时の需。废话不多说了,开始进入正题。高手就可以不用看了。仅是给新人看的。哈哈

  1.     加密目的:webservice最后部署了无非就是一个URL地址。只要知道地址就能够看到响应的结果。所以给webservice设置了访问用户和密码。增强安全性,使它只能被授权的用户使用。
  2. 集体实现过程如下:

        (1):打开VS2010建立解决方案(WService20150106)(PageBaseWebservice.asmx)(个人觉得VS在大了推荐大家使用WCF,还是什么其他原因没有在创建的时候直接找到webservice项目,可以先创建一个空的ASP.net 项目,在添加就能找到webservice)。代码如下注释很清楚。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Services;
 6 
 7 namespace Webservice2015
 8 {
 9     /// <summary>
10     /// PageBaseWebservice 的摘要说明
11     /// </summary>
12     [WebService(Namespace = "http://tempuri.org/")]
13     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
14     [System.ComponentModel.ToolboxItem(false)]
15     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
16     // [System.Web.Script.Services.ScriptService]
17     public class PageBaseWebservice : System.Web.Services.WebService
18     {
19 
20         public MySoapHeader _MySoapHeader;// 定义一个变量
21         /// <summary>
22         ///  重点是这个类是继承System.Web.Services.Protocols.SoapHeader 这是关键。加密就全靠这个了
23         /// </summary>
24         public class MySoapHeader : System.Web.Services.Protocols.SoapHeader
25         {
26             #region
27             /// <summary>
28             /// 用户名
29             /// </summary>
30             private string _name;
31             public string Name
32             {
33                 get { return _name; }
34                 set { _name = value; }
35             }
36             /// <summary>
37             /// 秘密
38             /// </summary>
39             private string _password;
40             public string Password
41             {
42                 get { return _password; }
43                 set { _password = value; }
44             }
45             #endregion
46 
47             /// <summary>
48             /// 无参数的构造函数
49             /// </summary>
50             public MySoapHeader()
51             {
52             }
53             /// <summary>
54             /// 带参数的构造函数
55             /// </summary>
56             /// <param name="Name">参数用户名</param>
57             /// <param name="Password">参数秘密</param>
58             public MySoapHeader(string Name, string Password)
59             {
60                 this.Name = Name;
61                 this.Password = Password;
62             }
63         }
64 
65         [WebMethod(Description = "验证权限", CacheDuration = 0, BufferResponse = true)]
66         [System.Web.Services.Protocols.SoapHeader("_MySoapHeader")]
67         public bool Validation()
68         {
69             /// 判断用户名 和密码 是否正确。 这里仅做测试。用户名和密码 可以写在配置文件中。
70             if (_MySoapHeader.Name == "KG" && _MySoapHeader.Password=="123456")
71                 // 匹配成功
72                 return true;
73             //匹配失败
74             return false;
75         }
76     }
77 }
View Code

 (2):在创建一个WebServiceTest.asmx。这个和(1)中PageBaseWebservice.asmx 是继承关系。PageBaseWebservice 继承自 System.Web.Services.WebService。WebServiceTest 是继承PageBaseWebservice。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Webservice2015
{
    /// <summary>
    /// WebServiceTest 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class WebServiceTest : PageBaseWebservice
    {
        [WebMethod(Description = "验证测试", CacheDuration = 0, BufferResponse = true)]
        // 加上标记
        [System.Web.Services.Protocols.SoapHeader("_MySoapHeader")]
        public string HelloWorld()
        {
            if (Validation())// 验证
            {
                return "Hello World";
            }
            else
            {
                return "你没权限";
            }
         
        }
    }
}
View Code

        (3):建立一个控制台程序。在添加引用。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            /// 服务
            ServiceReference1.WebServiceTestSoapClient webclient = new ServiceReference1.WebServiceTestSoapClient();
            /// soap 下面就是要传过过去用户名和密码
            ServiceReference1.MySoapHeader my = new ServiceReference1.MySoapHeader();
            my.Name = "KG";
            my.Password = "123456";
            /// my 做为一个参数传过去。
           string str=  webclient.HelloWorld(my);
           Console.Write(str);
           Console.ReadKey();
        }
    }
}
View Code

        (4):结果只有正确输入的时候才能返回helloword。否则返回的是没有权限。

 

转载于:https://www.cnblogs.com/mychuizi/p/4209458.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值