Skip to main content

Microsoft Silverlight

Answered Question Adding SOAP headers to ASMX web service messageRSS Feed

(0)

kobruleht
kobruleht

Member

Member

141 points

525 Posts

Adding SOAP headers to ASMX web service message

How to add User Name and password to SOAP ASMX webservice call message header ?

I tried MSDN sample from   

http://msdn.microsoft.com/en-us/library/8728chd5(VS.80).aspx

class MyHeader : SoapHeader

{

public string UserName;public string Password;

}

but got error since SoapHeader class is not defined.

How to fix ?  Can we use WCF OperationContext with ASMX web services or any other idea ?

Andrus.

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: Adding SOAP headers to ASMX web service message

Hi Andrus,

you have to use HttpContext, the following example permits to retrieve the header "Username":

[WebMethod]

public string Hello(String name)

{

string Username = String.Empty;

try

{

if (HttpContext.Current.Request.ServerVariables["HTTP_SOAPACTION"] != null)

{

// Load the body of the HTTP message

// into an XML document.

Stream HttpStream = HttpContext.Current.Request.InputStream;

HttpStream.Position = 0;

XmlDocument dom = new XmlDocument();

dom.Load(HttpStream);

// Bind to the Authentication header.

Username =

dom.GetElementsByTagName(
"Username").Item(0).InnerText;

}

}

catch { }

 

return "Hello " + Username;

}

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

kobruleht
kobruleht

Member

Member

141 points

525 Posts

Re: Adding SOAP headers to ASMX web service message

Davide,

Thank you.

You showed a way to retrieve message headers.

I asked for a way to add username/password to message from SL client application.

Btw. HttpContex class is not available in sl runtime.

Andrus.

 

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: Re: Adding SOAP headers to ASMX web service message

Hi Andrus,

for the client just use the code available here (solution SilverlightHeaders):

http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=silverlightws&DownloadId=3473

modify the SilverlightIpAddressMessageInspector class adding your personalized Headers:

public object BeforeSendRequest(ref Message request, IClientChannel channel)

{

request.Headers.Add(
MessageHeader.CreateHeader("Username", "", "Davide"));

 

return null;

}

Now insert a new asmx service in your web project to retrieve the Headers using the code I've posted in the previous post (here you have to use HttpContext).

Please let me know if the code works, otherwise I can send you a complete VS solution.

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP
Answered Question

Re: Re: Adding SOAP headers to ASMX web service message

I've posted a working solution here: http://www.davidezordan.net/blog/?p=658

Hope this helps.

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

integragreg
integragreg

Member

Member

16 points

3 Posts

Re: Re: Adding SOAP headers to ASMX web service message

David, your solution works very well except for one thing: what if the asmx web service is deployed on a production machine and you are not able to upgrade the server code to retrieve the headers from the httpcontext?

jacanon7
jacanon7

Member

Member

2 points

1 Posts

Re: Adding SOAP headers to ASMX web service message

 This is the answer to all my question.

 

 

Mr. I have found this for year, please help me can you pass your code to VB? My boss is going to kill me... Crying

 

Thanks a lot and God bless you.

 

Jorge andrés Cañón

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities