2011-02-27 163 views
5

我有一个期望soap头部并返回身份验证令牌的Webservice。我设法使用jQuery将soap头文件发布到webservice。问题是如何使浏览器在每个Web服务授权请求上发送经过身份验证的令牌。你的帮助将会很受欢迎。我用有用的链接如下: 参考:使用jQuery通过web服务进行SOAP身份验证

  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) 
    } 

回答

0

如果响应提供了一个积极的 - 发送令牌返回到您的服务器进行存储在会议中。

0

我有同样的情况,必须使用表单认证保护我的网站(包括网络服务)的一部分,并且还有一个未受保护的公共部分,例如,登录/注册表单,CSS文件,JS文件...

我通过指定可以处理您的登录功能的公共Web服务(不受表单身份验证保护)来解决登录问题。

你的登录功能,应该是这样的:

[WebMethod(EnableSession=true)] 
[ScriptMethod] 
public ResponseBase<bool> DoLogin(LoginCredentials Credentials) 
{ 
     try 
     { 
      // Perform login with credentials 

      if (loginOK) 
      { 
       FormsAuthentication.SetAuthCookie(/* Your user identification here */, true); 
      } 
      return new ResponseBase<bool> { Code= true}; 
     } 
     catch (Exception _ex) 
     { 
      // Save your log 
      return new ResponseBase<bool> { Message = "Incorrect Login" }; 
     } 
} 

您的客户端将收到的窗体身份验证cookie并登录你的回应。稍后,您可以通过评估响应中的代码属性来评估登录是否成功。

的ResponseBase的类如下所示:

[Serializable] 
[DataContract] 
public class ResponseBase<T> 
{ 
    /// <summary> 
    /// Return code. 
    /// </summary> 
    [DataMember] 
    public T Code { get; set; } 

    /// <summary> 
    /// Message. 
    /// </summary> 
    [DataMember] 
    public string Message { get; set; } 
} 

最后,但并非最不重要的,我建议你使用的不是XML JSON在零个改变通话,因为消息的重量轻的您的Web服务服务器代码。此外,在客户端中,您不必手动构建消息,就像在您的示例中一样。

相关问题