Hello,
I am working with Flex, Webservices and C# and I would like to secure the access on my web services through SOAP.
I spent 2 days on this problem:
My asmx file where i describe my 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;
}
Then I wrote a class to check if the content of SOAP Header is good.
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;
}
}
So far I think I'm right.
But while i want to implement it in Flex:
var q1:QName=new QName("http://localhost:59399/Service1.asmx?wsdl", "Header1");
header1=new SOAPHeader(q1, {String:"JOE",String JOE});
_serviceControl.addHeader(header1);
I get a NullReferenceException on my username, which seems to be not supply.
My Webservice works, except when I try to implement:
ServiceAuthHeaderValidation.Validate(CustomSoapHeader);
Could someone reply me in order to know what is missing ? or my mistake..
THank you for your time.
So far StackoverFlow helped me a lot by reading different answers but today I stay stuck on it. If someone can help.