2010-05-17 113 views
0

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

+0

您的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

回答

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定义里面看起来像的绑定更新,它不是。

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中生成。

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

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

无论如何。

如果有人知道通过更好的方式来掌握SOAP头部的方法。