SOAP Envelope Sample


Client Windows applicaton
------------------------------------------------------------
Public Class frmMain

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        TextBox1.Text = String.Empty

        Dim obj As New wsSampleSoap.AuthHeader
        Dim ws As New wsSampleSoap.Service1

        obj.UserName = "admin"
        obj.Password = "dotnet"

        ws.AuthHeaderValue = obj
        TextBox1.Text = ws.GetCustomerList()
    End Sub
End Class
-----------------------------------------------------------

** SOAP Web Service

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

using System.Web.Services.Protocols;
using System.Xml.Serialization;


namespace SampleSoapMessage
{

    public class AuthHeader : System.Web.Services.Protocols.SoapHeader
    {
        public string UserName;
        public string Password;
    }

    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/", Description="SOAP Header Example")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        public AuthHeader curUser;  // header item

        [WebMethod]
        [SoapHeader("curUser")]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod(Description = "authenticates a user")]
        [SoapHeader("curUser")]
        bool Login()
        {
            if (curUser != null)
            {
                if (Authenticate(curUser.UserName, curUser.Password))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
                return false;
           
        
        }

        bool Authenticate(string user, string pwd)
        {
            return user == "admin" && pwd == "dotnet";
        }

        [WebMethod(Description = "returns the current time")]
        [SoapHeader("curUser")]
        public string GetCustomerList()
        {
            string customerList = string.Empty;

            if (Login())
                customerList = "OK";
            else
                customerList = "Fail";

            return customerList;
        }

    }
}