webservice加上身份验证

1.添加请求头(server)

public class MySoapHeader:SoapHeader
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool ValideUser(string in_Username,string inPassword)
        {
            if(in_Username=="lmy"&&inPassword=="123456")
            { return true; }
            else { return false; }
        }
    }
方法名前面加上特性
 public MySoapHeader myheader = new MySoapHeader();
        [SoapHeader("myheader")]
        [WebMethod]
        public string Hi(string hi)
        {
此原理是通过发送请求后,调用SoapHeader特性的构造方法,为myheader对象中的username,password属性赋值

2.客户端调用(client)

var soapdata='{"subject": "测试电话李明远","cicc_contactid": null,"cicc_direction": "10","ownerid": "A6B254F4-6A49-E611-80BE-005056BD7FD2","cicc_contacts_text": "白展堂","cicc_if_im": null,"scheduleend": "2017-10-10 09:00:00","schedulestart": "2017-10-10 08:00:00","cicc_requestdepartment": null,"cicc_talktime": null,"description": null,"cicc_phonenumber": null,"cicc_company": null,"cicc_source": null}';
var username='lmy';var password='123456';
var soap='<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"><soap12:Header><MySoapHeader xmlns="http://tempuri.org/"><UserName>'+username+'</UserName><Password>'+password+'</Password></MySoapHeader></soap12:Header><soap12:Body><Hi xmlns="http://tempuri.org/"><hi>'+soapdata+'</hi></Hi></soap12:Body></soap12:Envelope>';
 $.ajax({
                type: "POST",
                url: "http://localhost:64568/WebService1.asmx?Hi",
                data:soap,
                contentType: "application/soap+xml",
                charset:"utf-8",
                cache: false,
                dataType:"xml",
                async: false,
                success: function (data) { debugger; alert(data.firstChild.textContent); },
                error: function (data) { console.log(data);}
            });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值