我正在使用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通過閱讀不同的答案幫助了我很多,但今天我仍然堅持下去。如果有人可以幫忙。
您的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