Skip to main content

Microsoft Silverlight

Answered Question Adding user name and password to SOAP message headerRSS Feed

(0)

kobruleht
kobruleht

Member

Member

141 points

525 Posts

Adding user name and password to SOAP message header

SL appl asks for user name and password. How to pass user name and password to SOAP WebService for custom validation?

I tried code below but got error on line

proxy.Headers.Add(header);

about missing Headers propery.

Andrus.

 

var proxy = new DocumentServiceReference.DocumentSoapClient();

MessageHeader header = MessageHeader.CreateHeader(typeof(UserCredentials).Name,

UserCredentials.WS_NAMESPACE,new UserCredentials("user", "password"), false);

proxy.Headers.Add(header);

proxy.SaveAssetDocumentCompleted += proxy_AddOrderCompleted;

proxy.SaveAssetDocumentAsync();

 

class UserCredentials

{

public const string WS_NAMESPACE = "urn:LOBApplicationFramework.Security";

public string UserName { get; set; }

public string Password { get; set; }

public UserCredentials()

{

}

public UserCredentials(string userName, string password)

{

UserName = userName;

Password = password;

}

}

 

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: Adding user name and password to SOAP message header

Hi,

check out this article:

http://www.silverlightshow.net/items/Building-a-Silverlight-Line-Of-Business-Application-Part-3.aspx

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

kobruleht
kobruleht

Member

Member

141 points

525 Posts

Re: Adding user name and password to SOAP message header

Thank you. I tried this code with SOAP Web Service.

In service line 

MessageHeaders messageHeaders = OperationContext.Current.IncomingMessageHeaders;

Causes NullReferenceException

Any idea how to make this code to work with SOAP web service ?

Andrus.

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: Re: Adding user name and password to SOAP message header

The source code of the article works fine for me.

However you can find here a simpler example using custom SOAP headers:

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

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

kobruleht
kobruleht

Member

Member

141 points

525 Posts

Re: Re: Adding user name and password to SOAP message header

Thank you. 

Have you tried with ASMX Web service?

I'm looking for a way to get message headers from ASMX web service, not from WCF web service.

Code sample you posted uses WCF web service.

Andrus.

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP

Re: Re: Re: Adding user name and password to SOAP message header

Hi Andrus,

I suggest you to use WCF services instead of "old" asmx, they are more scalable.

Once you have upgraded to WCF, use the last example I've posted, it works very well.

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

kobruleht
kobruleht

Member

Member

141 points

525 Posts

Re: Re: Re: Adding user name and password to SOAP message header

I'm planning to host web service in MONO. I have'nt found a way to run .svc service from MONO so I'm forced to use asmx which works OK.

I found the following article about retrieving SOAP headers in ASMX web service

http://www.codeproject.com/KB/cpp/authforwebservices.aspx

Should I use this method ?

Andrus.

davidezordan
davidezo...

Contributor

Contributor

5614 points

863 Posts

Silverlight MVP
Answered Question

Re: Re: Re: Re: Adding user name and password to SOAP message header

Check out this thread:

http://silverlight.net/forums/p/62643/155224.aspx

Thanks, Davide

Silverlight MVP

Blog Twitter Silverlight Experts

ljun
ljun

Member

Member

2 points

6 Posts

Re: Re: Re: Re: Adding user name and password to SOAP message header

So it seems like the best way to deal with this is to have a centralized method for calling web services.  Is that correct?

kobruleht
kobruleht

Member

Member

141 points

525 Posts

Re: Re: Re: Re: Adding user name and password to SOAP message header

My goal is to exchange the following info with ASMX service:

1. SQL Server login credentials (user name, password, database etc) 

2. ValidationException and other exception message texts.

I think that best way is not use message headers at all.

So I created special type

class MessageHeader {

public string User, Class, ExceptionText;

...

}

and pass this as  ref parameter to every web method call

List<Customer> GetCustomers( ref header, ... )

Ugly, but seems to be only solution.

Andrus.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities