我正在使用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