大家会了web服务,确实很好用,他可以通过定义soap头来定义自己的用户名密码,如下:




























public SecurityHeader header;
嘎嘎,注意了啊!
在service中添加处理方法
//用户名密码比对
private bool ValidateUser(string user, string pass)
{
if (user.Equals("user") && pass.Equals("user"))
return true;
else
return false;
}
下面才是要点(其实哪个都很重要)
[WebMethod, SoapHeader(" header")]
public string HelloWorld()
{
if (ValidateUser(npssa.UserName, npssa.UserPass))//比对之后返回
{
return "Hello World";
}
else
{
return "error";
}
}
服务端配置完了,客户端配置:
private myService.Service ms;
private void Init()
{
try
{
myService.SecurityHeader header = new Nowind.myService.SecurityHeader();//定义头
header.UserName = textBox1.Text;//用户名
header.UserPass = textBox2.Text;
ms = new Nowind.myService.Service();//定义web引用
ms.SecurityHeaderValue = header;//加入头信息
textBox3.Text=ms.HelloWorld();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
//也是别人写的自己看了,写出来的
如何让webservice的命名空间和方法在通过IE查看的时候不生成页面或者隐藏命名空间什么的呢.