好好学习,天天向上

--每天都有好心情
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

带验证信息的web服务

Posted on 2007-06-20 14:51  凤麟  阅读(188)  评论(0)    收藏  举报
 

为了安全,使用带验证的Web服务。Web服务是给予Soap协议和xml的。验证可以通过SOAP报头条目完成。如:

//soapheader 设置用户名和密码

     public class HeadAuthentication : SoapHeader

     {

         public string username;

         public string password;

     }

web 服务类中:

public[w1]   Authgeader myheader;

    [WebMethod]

    [SoapHeader("myheader",Direction=SoapHeaderDirection.In,Required=true)]

    public string HelloWorld() {

        if (myheader.user == "wwb" && myheader.pws == "123")

        {

            return "Hello World";

        }

        else

        {

            return "hello Guest";

        }

}

客户端:添加web服务

testws.Service myws = new WindowsApplication5.testws.Service();

testws.Authgeader myheader = new WindowsApplication5.testws.Authgeader()

  myheader.user = "wwb";

 myheader.pws = "123";

 myws.AuthgeaderValue[w2]  = myheader;

textBox1.Text=myws.HelloWorld();

 


 [w1]必须是public,如果不加的话,会出现错误。为什么这样?

 [w2]Soap头添加,必须有这个,否则会报错。