私は 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 ヘッダーの内容が適切かどうかをチェックするクラスを作成しました。
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 はさまざまな回答を読むことで私を大いに助けてくれましたが、今日はそれに固執しています。誰かが助けることができれば。