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、client引用:
//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、完毕

上一篇:MFC文档视图中窗口切换 (2012-05-11 18:32:48)


下一篇:mfc创建文档视图过程