WebService之Soap头验证入门

1、新建一个类,如"AuthHeaderUser",继承于"System.Web.Services.Protocols.SoapHeader"类

2、新建WebService,有一个AuthHeaderUser类型的属性

3、新建WebService的一个Web方法,指定Soap头信息,如:
[SoapHeader("AuthenticationUser")]
[WebMethod(Description="返回股票行情信息")]
public DataSet GetQuotedPrice()
{
if (AuthenticationUser != null) //step4,验证Soap头是否为空
{
//验证
WebServiceSoapHeaderAuthentication.DAL.LoginResult lr =
new WebServiceSoapHeaderAuthentication.DAL.UserDAL().Login(AuthenticationUser.UserId, AuthenticationUser.Password);
if (lr == LoginResult.Succeed)//登录成功
return new WebServiceSoapHeaderAuthentication.DAL.QuotedPriceDAL().GetQuotedPriceDataset();
else
throw new ApplicationException("Log in failed : " + lr.ToString());
}
else
throw new SoapHeaderException("......", SoapException.ClientFaultCode);
}

4、客户端引用:
//Web服务
localhost1583QuotedPriceService.QuotedPriceService service =
new WebServiceSoapHeaderAuthentication.QuotedPriceViewForm.localhost1583QuotedPriceService.QuotedPriceService();

//准备Soap头
WebServiceSoapHeaderAuthentication.BOL.User u = CurrentUser.getInstance();
localhost1583QuotedPriceService.AuthHeaderUser login_user =
new WebServiceSoapHeaderAuthentication.QuotedPriceViewForm.localhost1583QuotedPriceService.AuthHeaderUser();
login_user.UserId = u.UserId;
login_user.Password = u.UserPassword;

//Soap头赋值
service.AuthHeaderUserValue = login_user;

DataSet ds = service.GetQuotedPrice();
//todo:......

5、完成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值