我正在使用 Flex、Webservices 和 C#,我想通过 SOAP 保护对我的 Web 服务的访问。
我在这个问题上花了 2 天时间:
我描述我的 webmethod 的 asmx 文件:
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;
}
然后我写了一个类来检查SOAP 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 通过阅读不同的答案帮助了我很多,但今天我仍然坚持下去。如果有人可以提供帮助。