为WebService添加身份验证的两种方法

方法一:SoapHeader

辅助类:MySoapHeader 

 1     //SoapHeader 添加引用
 2     using System.Web.Services.Protocols;
 3     
 4     #region 配置登录标头
 5     
 6     public class MySoapHeader:SoapHeader
 7     {
 8         private string _strUserName = string.Empty;
 9         private string _strPassWord = string.Empty;
10 
11         #region 构造方法
12         public MySoapHeader() { }
13 
14         public MySoapHeader(string userName, string passWord)
15         {
16             _strUserName = userName;
17             _strPassWord = passWord;
18         }
19         #endregion
20 
21 
22         #region 构造用户名|密码
23         /// <summary>
24         /// 用户名
25         /// </summary>
26         public string UserName
27         {
28             get { return _strUserName; }
29             set { _strUserName = value; }
30         }
31         /// <summary>
32         /// 密码
33         /// </summary>
34         public string PassWord
35         {
36             get { return _strPassWord; }
37             set { _strPassWord = value; }
38         }
39         #endregion
40 
41 
42         #region 检测是否正常登录
43         public bool CheckLogin()
44         {
45             if (_strUserName == "hkl" && _strPassWord == "123")
46             {
47                 return true;
48             }
49             else return false;
50         }
51         #endregion
52 
53     }
54 
55     #endregion
View Code

WebService代码

 1         public MySoapHeader myHeader = new MySoapHeader();
 2 
 3         [System.Web.Services.Protocols.SoapHeader("myHeader")]
 4         [WebMethod(Description = "判断用户是否开通", EnableSession = true)]
 5         public string GetValue(string strInputValue)
 6         {
 7             if (myHeader.CheckLogin())
 8             {
 9                 string strReturnValue = strInputValue + "@身份验证已通过";
10                 return strReturnValue;
11             }
12             else return "身份无效,请重试!";
13         }
View Code

新建一个web网页并添加web引用,在pageload方法中添加如下代码

1             localhost1.MySoapHeader myHeader = new localhost1.MySoapHeader();
2             myHeader.UserName = "hkl";
3             myHeader.PassWord = "123";
4 
5             localhost1.Service1 myTest = new localhost1.Service1();
6             myTest.MySoapHeaderValue = myHeader;
7             Response.Write(myTest.GetValue("This's my test Application for SoapHeader."));
View Code

运行即可查看结果。

 

方法二:Session

  WebService代码

 1         [WebMethod(Description = "检测是否通过验证", EnableSession = true)]
 2         public bool CheckLogin(string strUserName, string strPassWord)
 3         {
 4             if (strUserName.Equals("xxx") && strPassWord.Equals("123"))
 5             {
 6                 Session["LoginState"] = true;
 7             }
 8             else Session["LoginState"] = false;
 9 
10             return (bool)Session["LoginState"];
11         }
12 
13         [WebMethod(Description = "测试连接", EnableSession = true)]
14         public string GetValue(string strInputValue)
15         {
16             if (Session["LoginState"] == null || Session["LoginState"].Equals(false))
17             {
18                 return "无效身份,请重试!";
19             }
20             else
21             {
22                 string strReturnValue = strInputValue + "@身份验证已通过";
23                 return strReturnValue;
24             }
25         }
View Code

新建一个web网页并添加web引用,在pageload方法中添加如下代码

1             localhost2.Service2 myTest = new localhost2.Service2();
2             myTest.CookieContainer = new System.Net.CookieContainer();
3             if (myTest.CheckLogin("xxx", "123"))
4             {
5                 Response.Write(myTest.GetValue("This is my test application for session."));
6             }
View Code

运行即可查看结果。

转载于:https://www.cnblogs.com/catherinehu/p/8459217.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值