WebService 简单安全验证

本文介绍了一种通过在WebService中使用自定义SoapHeader类来实现简单的安全验证方法。具体做法是创建一个包含安全密钥的SecurityHeader类,并将其作为SoapHeader添加到WebService方法中。当客户端调用这些方法时,服务器会检查提供的密钥是否正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文: WebService 简单安全验证

        最近新接了一个需要调用第三方WebService的项目,看到这个第三方WebService被调用的时候,需要授权用户名和密码,于是自己也想对WebService的安全授权这个方面进行了一下研究,以前调用的WebService大部分都是局域网内部调用,几乎没有什么权限需要增加的,今天借此机会,深入研究了一下,发现实现起来还是挺容易的。

       基本原理就是利用SoapHeader 类,继承该类,然后在我们公布的方法上加上对应的标签,呵呵。现在做一个Demo程序,进行验证。我们首先衍生一个自己的子类,暂命名为SecurityHeader ,在该类中需要增加一个公共属性,详看如下代码

 

  public class SecurityHeader : SoapHeader

    {
        
public   string  SecurityKey
        {
            
get ;
            
set ;
        }
    }

 

      在WebService 中对该SoapHeader 的调用实现,也是比较容易理解的,详看一下代码就可以很好的理解了,代码中只需对SoapHeader进行验证就可以了。如果我们不增加SoapHeader,其实质就是普通的Public 类型的WebService 。调用的时候完全的公开的,不需要任何的验证信息。

 

  ///   <summary>
    
///  Summary description for Integration
    
///   </summary>
    [WebService(Namespace  =   " http://tempuri.org/ " )]
    [WebServiceBinding(ConformsTo 
=  WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
false )]
    
//  To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    
//  [System.Web.Script.Services.ScriptService]
     public   class  Integration : System.Web.Services.WebService
    {
        
public  SecurityHeader securityKey  =   new  SecurityHeader();

        [WebMethod]
        [SoapHeader(
" securityKey " )]
        
public   string  HelloWorld( string  show)
        {
            
if  (securityKey.SecurityKey.Equals( " 850 " ))
            {
                
return   " This is security webservice  "   +  show;
            }
            
else
            {
                
return   " Sorry,You didn't permissions! " ;
            }
        }

        [WebMethod]
        
public   string  HelloPanda( string  show)
        {
            
return   " This is public webservice   "   +  show;
        }

    } 

   

       验证程序代码如下:

     

  class  Program
    {
        
static   void  Main( string [] args)
        {
            PandaRGIntegration.SecurityHeader header 
=   new  PandaRG.Listrak.PandaRGIntegration.SecurityHeader();
            header.SecurityKey 
=   " 850 " ;

            PandaRGIntegration.IntegrationSoapClient client 
=   new  PandaRG.Listrak.PandaRGIntegration.IntegrationSoapClient();
            System.Console.WriteLine(client.HelloWorld(header,
" Vincent " ));
            System.Console.WriteLine(client.HelloPanda(
" Vincent " ));
            System.Console.Read();
        }
    }

 

调用结果: 


 

      当我们修改 header.SecurityKey = "8500"时结果

 

       整个完整例子到此实验完毕。感性趣的,也可以自己试验一下。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值