MarkService.MarkServiceSoapClient markService = new Fadmad.ManageCenter.MarkService.MarkServiceSoapClient();
MarkService.CredentialSoapHeader Head = new Fadmad.ManageCenter.MarkService.CredentialSoapHeader();
//WebService头验证
Head.UserName = "WebServiceUser";
Head.PassWord = "WebServicePwd";
//调用WebService方法,加入Head 头验证
object[] list = markService.GetProductPrice(Head, UserID, barCodeID, nm);
//以下为WebService 代码
//配置文件内容如下:
<appSettings>
<add key="WebServiceUser" value="WebServiceUser"></add>
<add key="WebServicePwd" value="WebServicePwd"></add>
</appSettings>
//验证类文件 ,(单独放到一个类文件里面吧)
public class CredentialSoapHeader : System.Web.Services.Protocols.SoapHeader
{
private string _UserName = string.Empty;
private string _PassWord = string.Empty;
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
public string PassWord
{
get { return _PassWord; }
set { _PassWord = value; }
}
public void Initial(string nUserName, string nPassWord)
{
UserName = nUserName;
PassWord = nPassWord;
}
public bool IsValid(string nUserName, string nPassWord)
{
try
{
string UserName = System.Configuration.ConfigurationSettings.AppSettings["WebServiceUser"].ToString();
string UserPwd = System.Configuration.ConfigurationSettings.AppSettings["WebServicePwd"].ToString();
//判断用户名密码是否正确
if (nUserName == UserName && nPassWord == UserPwd)
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
/// <summary>
/// 用户名密码是否正确
/// </summary>
/// <returns>用户名密码是否正确</returns>
public bool IsValid()
{
return IsValid(_UserName, _PassWord);
}
}
public CredentialSoapHeader myHeader = new CredentialSoapHeader();
#region 以下WebService方法
[SoapHeader("myHeader")]
[WebMethod(Description = "加入了身份验证的web服务", EnableSession = true)]
public ArrayList GetProductPrice(int UserID, int BarCodeID, int Count)
{
if (myHeader.IsValid())
{
return mMark.GetProductPrice(UserID, BarCodeID, Count);
}
else
{
throw (new Exception("身份验证失败"));
}
}
#endregion