為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

運行即可查看結果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM