SOAP 标头:Flex 和 C# 之间的通信受到保护

发布于 2024-09-01 20:16:59 字数 2125 浏览 5 评论 0原文

我正在使用 Flex、Webservices 和 C#,我希望通过 SOAP 保护对我的 Web 服务的访问。

我花了 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;
    }

然后我写了一个类来检查是否 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,这似乎没有提供。

我的网络服务可以正常工作,除非我尝试实现:

ServiceAuthHeaderValidation.Validate(CustomSoapHeader);

有人可以回复我以了解缺少什么吗?或者是我的错误..

谢谢您的宝贵时间。

到目前为止,StackoverFlow 通过阅读不同的答案对我帮助很大,但今天我仍然坚持下去。如果有人可以帮忙的话。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红尘作伴 2024-09-08 20:16:59

我只使用自定义 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 定义中使用 {}看起来是更新的绑定,但事实并非如此。

I just use a custom 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;
    }

Keep in mind that using {} inside an E4X definition looks like a binding that updates, it's not.

铃予 2024-09-08 20:16:59

非常感谢,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);

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

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

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

反正。

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

Thanks a lot, Sophistifunk

I finally added in my generated AS sub class.

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);

But when we implement a new WebService in Flex 4 through its IDE, everything is generated in AS.

But The way to manage the header sucks really.

That should take in charge automatically the security Header.. I mean create a function setLogin or setpassword bindable from or mxml application.

Anyway.

If someone know a way to take in charge the Header of SOAP through a better way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文