调用WebService时加入身份验证,以拒绝未授权的访问

调用WebService时加入身份验证,以拒绝未授权的访问

分类: WebService 548人阅读 评论(4) 收藏 举报

      众所周知,WebService是为企业需求提供的在线应用服务,其他公司或应用软件能够通过Internet来访问并使用这项在线服务。但在有些时候的某些应用服务不希望被未授权访问,那么此时我们可以一下几种方法来实现身份验证。

方法一:在WebService中引入SoapHeader

  1. #region 配置登录标头   
  2. /// <summary>   
  3. /// Code CreateBy BanLao   
  4. /// </summary>   
  5. public class MySoapHeader : SoapHeader  
  6. {  
  7.     private string strUserName = string.Empty;  
  8.     private string strPassWord = string.Empty;  
  9.   
  10.     public MySoapHeader() { }  
  11.   
  12.     public MySoapHeader(string username, string password)  
  13.     {  
  14.         this.strUserName = username;  
  15.         this.strPassWord = password;  
  16.     }  
  17.  
  18.     #region 构造 用户名|密码   
  19.     /// <summary>   
  20.     /// 用户名   
  21.     /// </summary>   
  22.     public string UserName  
  23.     {  
  24.         get { return strUserName; }  
  25.         set { strUserName = value; }  
  26.     }  
  27.     /// <summary>   
  28.     /// 密码   
  29.     /// </summary>   
  30.     public string PassWord  
  31.     {  
  32.         get { return strPassWord; }  
  33.         set { strPassWord = value; }  
  34.     }  
  35.  
  36.     #endregion  
  37.  
  38.     #region 检测是否正确登录   
  39.     /// <summary>   
  40.     /// 检测是否正确登录   
  41.     /// </summary>   
  42.     /// <returns></returns>   
  43.     public bool CheckLogin()  
  44.     {  
  45.         if (strUserName == "合法登录名" && strPassWord == "合法登录密码")  
  46.         {  
  47.             return true;  
  48.         }  
  49.         else  
  50.         {  
  51.             return false;  
  52.         }  
  53.     }  
  54.  
  55.     #endregion   
  56. }  
  57. #endregion  

加入一个服务用于测试:

  1. #region 测试连接   
  2.  [System.Web.Services.Protocols.SoapHeader("myHeader")]  
  3.  [WebMethod(Description = "判断用户是否开通", EnableSession = true)]  
  4.  public string _GetValue(string strInputValue)  
  5.  {  
  6.      if (myHeader.CheckLogin())  
  7.      {  
  8.          string strReturnValue = strInputValue + "@CopyRight By BanLao 2010";  
  9.          return strReturnValue;  
  10.      }  
  11.      else  
  12.      {  
  13.          return "无效的身份验证,请重试!";  
  14.      }  
  15.  }  
  16.  #endregion  

至此我们想要的需要通过身份验证的服务配置好了,下面让我们进行一些测试,新建一个webForm在Page_Load中:

  

  1. WebLogon.MySoapHeader myHeader = new WebLogon.MySoapHeader();  
  2. myHeader.UserName = "约定的合法用户";  
  3. myHeader.PassWord = "约定的合法密码";  
  4.   
  5. WebLogon.Service This_Service = new WebLogon.Service();  
  6. This_Service.MySoapHeaderValue = myHeader;  
  7. Response.Write(This_Service._GetValue("This is BanLao's Test Application For SoapHeader. "));  

当运行这个WebForm时,如果用户名和密码是正确的我们将看到:

This is BanLao's Test Application For SoapHeader. @CopyRight By BanLao 2010

否则

无效的身份验证,请重试!

方法二:Web Service以Session方式验证

 

  1. [WebMethod(Description = "检测是否正确登录", EnableSession = true)]  
  2. public bool CheckLogin(string strUserName, string strPassword)  
  3. {  
  4.     if (strUserName.Equals("admin") && strPassword.Equals("123456"))  
  5.     {  
  6.         Session["LoginState"] = true;  
  7.     }  
  8.     else  
  9.     {  
  10.         Session["LoginState"] = false;  
  11.     }  
  12.     return (bool)Session["LoginState"];  
  13. }  
  14.  
  15. #region 测试连接   
  16. [WebMethod(Description = "测试连接", EnableSession = true)]  
  17. public string _GetValue(string strInputValue)  
  18. {  
  19.     if (Session["LoginState"] == null || Session["LoginState"].Equals(false))  
  20.     {  
  21.         return "无效的身份验证,请重试!";  
  22.     }  
  23.     else  
  24.     {  
  25.         string strReturnValue = strInputValue + "@CopyRight By BanLao 2010";  
  26.         return strReturnValue;  
  27.     }  
  28. }  
  29. #endregion  

调用该服务,

  1. WebLogon.Service This_Service = new WebLogon.Service();  
  2. This_Service.CookieContainer = new System.Net.CookieContainer();  
  3. if (This_Service.CheckLogin("admin""123456"))  
  4. {  
  5.     Response.Write(This_Service._GetValue("This is BanLao's Test Application For Session. "));  
  6. }  

当运行这个WebForm时,如果用户名和密码是正确的我们将看到:

This is BanLao's Test Application For Session. @CopyRight By BanLao 2010

否则

无效的身份验证,请重试!

注:如果需要多个合法用户,可以在WebService中声明判断即可~

原文地址:https://www.cnblogs.com/fx2008/p/2532139.html