2

我们有一个 WCF 服务、wsHttpBinding 和 CustomBasicAuthentication。客户端是一个 Windows 窗体应用程序,它使用指定的用户名/密码工作。

现在我们需要创建另一个客户端,这将是 html 页面。我正在尝试使用 xmlHttpRequest 调用服务,但出现以下错误:

http://www.w3.org/2005/08/addressing/soap/faults:Sendera:BadContextToken消息无法处理。这很可能是因为“http://tempuri.org/IService/SayHello”操作不正确,或者因为消息包含无效或过期的安全上下文令牌,或者因为绑定之间不匹配。如果服务由于不活动而中止通道,则安全上下文令牌将无效。为了防止服务过早中止空闲会话,请增加服务端点绑定的接收超时。

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function CallServer() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open('POST', 'http://localhost:56779/BeSTService.svc', true,"umais","asghar");
        xmlhttp.setRequestHeader("Content-type", "application/soap+xml");
        var sr ='<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">'+
                  '<s:Header>'+
                    '<a:Action s:mustUnderstand="1">http://tempuri.org/IService/SayHello</a:Action>'+                    
                    '<a:ReplyTo>'+
                      '<a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>'+
                    '</a:ReplyTo>' +                        
                  '</s:Header>'+
                  '<s:Body>'+
                    '<SayHello xmlns="http://tempuri.org/">'+
                        '<name>Umais</name>'+
                    '</SayHello>'+
                  '</s:Body>'+
                '</s:Envelope>';
        xmlhttp.send(sr);
    }
</script>
</head>
<body>
<div style="margin:50px">
    <input type="button" value="Call Service" onclick="CallServer();" />
</div>
</body>
</html>

我还尝试在 Soap 消息中添加安全标头:

'<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">' +
    '<o:UsernameToken>' +
        '<o:Username>umais</o:Username>' +
    '<o:Password>asghar</o:Password>' +
        '</o:UsernameToken>' +
'</o:Security>' +

但同样的错误,在 Windows 窗体应用程序中使用 IClientMessageInspector 时,发送的 SOAP 消息与我在 HTML 中使用的完全相同,没有安全标头

请帮忙

4

0 回答 0