2010-05-17 51 views
0

我正在使用Flex,Webservices和C#,並且我想通過SOAP保護我的Web服務的訪問。SOAP頭:Flex和C#之間的通信安全#

我花了2天這個問題:

我ASMX文件,其中我描述了我的webmethod:

public ServiceAuthHeader CustomSoapHeader = new ServiceAuthHeader(); 

    [SoapHeader("CustomSoapHeader")] 
    [WebMethod(Description = "Return Somme")] 
    public int getTotal(int x, int y) 
    { 
     ServiceAuthHeaderValidation.Validate(CustomSoapHeader); 
     var total = x+y; 
     return total; 
    } 


    public class ServiceAuthHeader : SoapHeader 
    { 
     // SoapHeader for authentication 
     public string Username; 
     public string Password; 
    } 

然後我寫了一個類來檢查 內容Header的是 好。

public class ServiceAuthHeaderValidation 
{ 

[SoapHeader("soapHeader")] 
[WebMethod(Description = "Check Header")] 
public ServiceAuthHeader Validate(ServiceAuthHeader soapHeader) 
{ 
    if (soapHeader == null) 
    { 
     throw new NullReferenceException("No soap header was specified.");      
    } 
    if (soapHeader.Username ==null) 
    { 
     Console.WriteLine(soapHeader.Username); 
     throw new NullReferenceException("Username was not supplied for authentication in SoapHeader!"); 
    } 
    if (soapHeader.Password == null) 
    { 
     throw new NullReferenceException("Password was not supplied for authentication in SoapHeader."); 
    } 

    if (soapHeader.Username != "JOE") || soapHeader.Password != "JOE") 
    { 
     throw new Exception("Please pass the proper username and password for this service."); 


    } 
    return true; 
} 

}

到目前爲止,我認爲我是對的。

可是當我要實現它的Flex:

var q1:QName=new QName("http://localhost:59399/Service1.asmx?wsdl", "Header1"); 
     header1=new SOAPHeader(q1, {String:"JOE",String JOE}); 
     _serviceControl.addHeader(header1); 

我得到一個NullReferenceException在我的用戶名,這似乎是不提供。

我的web服務工作,除了當我嘗試實施:

ServiceAuthHeaderValidation.Validate(CustomSoapHeader); 

可能有人回覆了我才能知道缺什麼?或我的錯誤..

謝謝你的時間。

到目前爲止,StackoverFlow通過閱讀不同的答案幫助了我很多,但今天我仍然堅持下去。如果有人可以幫忙。

+0

您的Flex代碼不會讓我繼續下去。你是否在QName組件中使用了WebService標籤?您是否閱讀過有關向Flex中的Soap請求添加頭文件的文檔? http://www.adobe.com/livedocs/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=dataservices_099_32.html – JeffryHouser 2010-05-17 17:49:47

回答

0

我只是使用自定義XML:

public function addUserCredentials(username:String, passwordHash:String):ISoapInvocation 
    { 
     var headerDetails:XML = 
      <Security xmlns={wsse}> 
       <UsernameToken> 
        <Username>{username}</Username> 
        <Password>{passwordHash}</Password> 
       </UsernameToken> 
      </Security>; 

     securityHeader = new SOAPHeader(securityQName, headerDetails); 

     return this; 
    } 

請記住,使用{}的E4X定義裏面看起來像的綁定更新,它不是。

0

非常感謝,Sophistifunk

我終於在我生成的AS子類中添加了。

var header1:SOAPHeader; 
     var q1:QName = new QName("http://localhost:80/", "Header");  
     header1 = new SOAPHeader(q1, {}); 
     var a:XML = <AuthHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost:80/"> 
        <UserLogin>Joe</UserLogin> 
        <mdp>test</mdp> 
        </AuthHeader> 
     header1.content = a; 
     _serviceControl.addHeader(header1); 

但是當我們通過IDE在Flex 4中實現新的WebService時,所有東西都在AS中生成。

但管理標題的方式真的很糟糕。

這應該自動負責安全標題..我的意思是創建一個函數setLogin或setpassword可綁定或mxml應用程序。

無論如何。

如果有人知道通過更好的方式來掌握SOAP頭部的方法。