SOAP 标头在 ASMX Web 服务中如何工作?

发布于 2024-11-05 02:30:05 字数 155 浏览 6 评论 0原文

我有一个项目使用带有 [WebMethods] 和 [SoapHeader("Auth")] 的 asmx 文件。我没有使用 SOAP 的经验,也不明白它是如何工作的。

通过查看代码,我注意到您可以使用与标题同名的变量,并且它包含数据。数据是如何发送到header的?它从哪里来?

I have a project that uses an asmx file with [WebMethods] and [SoapHeader("Auth")]. I have no experience using SOAP and don't understand how it works yet.

Going through the code I noticed that you can use a variable with the same name as the header and it contains the data. How is the data sent to the header? Where does it come from?

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

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

发布评论

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

评论(3

不再让梦枯萎 2024-11-12 02:30:05

通过使用从 SoapHeader 派生的类,在标头中发送数据。该类将被声明为您的 webservice 类中的一个属性。然后,在您的 Web 方法中,您将在处理实际方法之前检查此属性中的身份验证信息。

可以在这里找到一个简单的实现 http://www.codeproject.com/KB/cpp/ authforwebservices.aspx

以下 msdn 链接讲述了另一种类似的技术,该技术会更复杂http://msdn.microsoft.com/en-us/library/9z52by6a.aspx。

在标头中传递数据的基本思想保持不变。

The data is sent in header by making use of a class that derives from SoapHeader. This class will be declared as a property in your webservice class. Then in your web method you will check the authentication information in this property before processing the actual method.

A simple implementation can be found here http://www.codeproject.com/KB/cpp/authforwebservices.aspx

The following msdn link tells about another similar technique, which would be more sophisticated one http://msdn.microsoft.com/en-us/library/9z52by6a.aspx.

Basic idea behind passing data in header remains same.

自在安然 2024-11-12 02:30:05

数据来自 SOAP 信封的 部分中的 XML。

The data comes from XML within the <soap:Header> section of the SOAP envelope.

入画浅相思 2024-11-12 02:30:05

像平常一样为您的肥皂头创建一个类。

public class AuthHeader : SoapHeader
{
    public string CompanyID;
    public string Username;
    public string Password;
}

然后在你的正常班级中有参考资料。

public class MyClass : WebService
{
    public readonly AuthHeader authHeader;

    [SoapHeader("authHeader", Direction = SoapHeaderDirection.In)]
    [WebMethod(CacheDuration = 20
        , EnableSession = true
        , Description = "Find stuff now."
        , MessageName = "FindStuff")]
    [ScriptMethod(UseHttpGet = false
        , ResponseFormat = ResponseFormat.Xml
        , XmlSerializeString = true)]

    public MyResponseClass FindStuff(string searchString)
    {
        MyResponseClass myResponseClass = new MyResponseClass();
        if (authHeader.Username == "myUser" &&
            authHeader.Password == "myPass" &&
            authHeader.CompanyID == "BobsTire")
        {
            ....
            myResponseClass = ....
        }
        return myResponseClass;
    }
}

Create a class for your soap header like normal.

public class AuthHeader : SoapHeader
{
    public string CompanyID;
    public string Username;
    public string Password;
}

Then within your normal class had the reference.

public class MyClass : WebService
{
    public readonly AuthHeader authHeader;

    [SoapHeader("authHeader", Direction = SoapHeaderDirection.In)]
    [WebMethod(CacheDuration = 20
        , EnableSession = true
        , Description = "Find stuff now."
        , MessageName = "FindStuff")]
    [ScriptMethod(UseHttpGet = false
        , ResponseFormat = ResponseFormat.Xml
        , XmlSerializeString = true)]

    public MyResponseClass FindStuff(string searchString)
    {
        MyResponseClass myResponseClass = new MyResponseClass();
        if (authHeader.Username == "myUser" &&
            authHeader.Password == "myPass" &&
            authHeader.CompanyID == "BobsTire")
        {
            ....
            myResponseClass = ....
        }
        return myResponseClass;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文