WebService 加入安全验证

        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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值