zoukankan      html  css  js  c++  java
  • 为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

    运行即可查看结果。

  • 相关阅读:
    mybatis:mybatis再总结
    shiro:RememberMe
    shiro:session管理
    shiro:缓存管理
    shiro:授权管理
    shiro:密码加密(加密、加盐加密)
    spring:spring再总结(ioc、aop、DI等)
    SpringBoot:整合layui、退出功能
    layui:内置模块(日期与时间、数据表格)
    nuxtjs中配置配置env
  • 原文地址:https://www.cnblogs.com/catherinehu/p/8459217.html
Copyright © 2011-2022 走看看