Skip to main content
 首页 » 编程设计

jquery之使用 jQuery 通过 Web 服务进行 SOAP 身份验证

2026年10月29日6jirigala

我有一个 Web 服务,需要一个 SOAP 头并返回一个身份验证 token 。我已经设法使用 jquery 将 SOAP 头发布到网络服务。问题是如何让浏览器在每个通过 Web 服务进行授权的请求上发送经过身份验证的 token 。我们将非常感谢您的帮助。我使用的有用链接如下: 引用:

  1. Securing ASP.Net Web Service using SOAP

  2. Calling Webservice from Jquery (Posting SOAP Header)

代码:

function logIn(username, password, token) { 
        var soapMessage = 
        '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \ 
        <soap:Body> \ 
        <SecuredWebServiceHeader  xmlns="http://tempuri.org/"> \ 
        <Username>' + username + '</Username> \ 
        <Password>' + password + '</Password> \ 
        <AuthenticatedToken>' + token + '</AuthenticatedToken> \ 
        </SecuredWebServiceHeader> \ 
        </soap:Body> \ 
        </soap:Envelope>'; 
 
        $.ajax({ 
            url: "http://localhost/wstest/Service.asmx/AuthenticateUser", 
            type: "POST", 
            dataType: "xml", 
            data: soapMessage, 
            complete: endLogin, 
            contentType: "text/xml; charset=\"utf-8\"" 
        }); 
 
        return false; 
    } 
 
    function endLogin(xmlHttpRequest, status) { 
        alert(xmlHttpRequest.responseXML) 
    } 

请您参考如下方法:

如果响应提供肯定 - 将该 token 发送回您的服务器以存储在 session 中。