WebService中的用户验证问题

   
    
  <configuration>  
   
      <system.web>  
          <authentication   mode="Windows"/>  
      </system.web>  
   
      <location   path="secureservice.asmx">  
   
          <system.web>  
              <authorization>  
                  <allow   users="Administrator"/>  
                  <allow   users="DOMAIN/Bradley"/>  
                  <deny   roles="BUILTIN/Power   Users"/>  
              </authorization>  
          </system.web>  
   
      </location>  
   
  </configuration>  
   
  下面是一个使用SOAPHeader的例子:  
  soapheaders.aspx      
  <%@   Import   Namespace="SoapHeaders"   %>  
   
      <script   language="C#"   runat="server">  
   
          public   void   Page_Load(Object   sender,   EventArgs   e)   {  
   
                  Response.Write("<h4><font   face=/"verdana/">Using   Soap   Headers   for   Custom   Authentication</font></h4>");  
   
                  //   Create   a   new   instance   of   the   UsingSoapHeaders  
                  //   proxy   class   used   to   call   the   remote   .asmx   file  
                  HeaderService   h   =   new   HeaderService();  
   
                  //   Call   the   secure   method   without   credentials  
                  Response.Write("First   call   result:   <p>");  
                  try   {  
                      Response.Write(h.SecureMethod()   +   "<p>");  
                  }  
                  catch   (Exception   ex)   {  
                      Response.Write("<pre>"   +   ex.StackTrace   +   "</pre><p>");  
                  }  
   
                  //   Create   a   new   instance   of   the   AuthHeader   class  
                  AuthHeaderCS   myHeader   =   new   AuthHeaderCS();  
   
                  //   Set   the   value   of   myHeader  
                  myHeader.Username   =   "JohnDoe";  
                  myHeader.Password   =   "password";  
   
                  //   Set   the   AuthHeader   public   member   of   the  
                  //   UsingSoapHeaders   class   to   myHeader  
                  h.AuthHeaderCSValue   =   myHeader;  
   
                  //   Call   the   secure   method   with   credentials  
                  Response.Write("Second   call   result:   <p><pre>"   +   h.SecureMethod()   +   "</pre>");  
          }  
   
      </script>  
   
   
  soapheaders.asmx:  
   
  <%@   WebService   Language="C#"   Class="SoapHeadersCS.HeaderService"   %>  
   
  using   System;  
  using   System.Web.Services;  
  using   System.Web.Services.Protocols;  
   
  //   Note   the   namespace   has   to   be   different   from   the   one   used  
  //   on   the   proxy   dll   or   we   get   errors   about   AuthHeader   being  
  //   defined   in   multiple   places.  
  namespace   SoapHeadersCS   {  
   
            //   AuthHeader   class   extends   from   SoapHeader  
          public   class   AuthHeaderCS   :   SoapHeader   {  
                  public   string   Username;  
                  public   string   Password;  
          }  
   
          [WebService(Description="Simple   sample   to   demonstrate   use   of   SOAP   Headers")]  
          public   class   HeaderService   {  
   
                  public   AuthHeaderCS   sHeader;  
   
                  [WebMethod(Description="This   method   requires   a   custom   soap   header   set   by   the   caller")]  
                  [SoapHeader("sHeader")]  
                  public   string   SecureMethod()   {  
   
                          if   (sHeader   ==   null)  
                              return   "ERROR:   Please   supply   credentials";  
   
                          string   usr   =   sHeader.Username;  
                          string   pwd   =   sHeader.Password;  
   
                          if   (AuthenticateUser(usr,   pwd))   {  
                                    return   "SUCCESS:   "   +   usr   +   ","   +   pwd;  
                          }  
                          else   {  
                                    return   "ERROR:   Could   not   authenticate";  
                          }  
                  }  
   
                  private   bool   AuthenticateUser(string   usr,   string   pwd)   {  
   
                          if   ((usr   !=   null)&&(pwd   !=   null))   {  
                                  //   could   query   a   database   here   for   credentials...  
                                  return   true;  
                            }  
                          return   false;  
                  }  
          }  
  }   
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值