0

我正在使用 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 通过阅读不同的答案帮助了我很多,但今天我仍然坚持下去。如果有人可以提供帮助。

4

2 回答 2

0

我只是使用自定义 XML:

    public function addUserCredentials(username:String, passwordHash:String):ISoapInvocation
    {
        var headerDetails:XML =
            <Security xmlns={wsse}>
                <UsernameToken>
                    <Username>{username}</Username>
                    <Password>{passwordHash}</Password>
                </UsernameToken>
            </Security>;

        securityHeader = new SOAPHeader(securityQName, headerDetails);

        return this;
    }

请记住,在 E4X 定义中使用 {}看起来像是更新的绑定,但并非如此。

于 2010-05-18T01:38:52.863 回答
0

非常感谢, Sophistifunk

我终于在我生成的 AS 子类中添加了。

var header1:SOAPHeader;
        var q1:QName = new QName("http://localhost:80/", "Header");     
        header1 = new SOAPHeader(q1, {});
        var a:XML = <AuthHeader xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost:80/">
                    <UserLogin>Joe</UserLogin> 
                    <mdp>test</mdp> 
                    </AuthHeader>
        header1.content =  a;
        _serviceControl.addHeader(header1);

但是当我们通过它的 IDE 在 Flex 4 中实现一个新的 WebService 时,一切都是在 AS 中生成的。

但是管理标题的方式真的很糟糕。

这应该自动负责安全标头。我的意思是创建一个可从或 mxml 应用程序绑定的函数 setLogin 或 setpassword。

反正。

如果有人知道如何通过更好的方式来管理 SOAP 的 Header。

于 2010-05-18T14:02:13.620 回答