views:

503

answers:

1

Hi,

Im wondering how to apply HttpAuth on a C# SoapClient(WebService proxy).

I have googled and read a lot that you can set the Credentials property(applying a NetworkCredentials object). But I cant seem to find this property on this object..

Would be greatful for any help,

//Robin

A: 

Robin,

The first step would be to actually generate a proxy class for your web service using the SDK command line. This can be done by opening the command window and typing wsdl /out:myProxyClass.cs http://hostServer/WebserviceRoot/WebServiceName.asmx?WSDL.

If you've already done that then you're a step ahead. Next you need to add that newly created .cs to your project. Generally I then recommend you create a clientconfig class or something along those lines that will allow you to access the methods available through the web service. For example:

 public class WSClientConfig
 {

    public string endpoint = "";

    public string username = "";

    public string password = "";

    public PurgeFilesServiceWse service;



    #region Constructor

public LLClientConfig(string endpoint, string username, string password)
{
      if (endpoint == null || username == null || password == null) 
      {
 Console.WriteLine("Usage: ENDPOINT, USERNAME, PASSWORD");
              Console.WriteLine("One of these usage items was NULL!");
 return ;
      }

        this.endpoint = endpoint;
        this.username = username;
        this.password = password;

   CreateService();
    }
    #endregion


    #region Class Methods
    /// <summary>
    /// Create the connection to the web service.
    /// </summary>
    protected void CreateService()
    {
        UsernameToken token = new UsernameToken(username, password,   PasswordOption.SendHashed);
        service = new PurgeFilesServiceWse();
        service.Url = endpoint;

        // Create a new policy.
        Policy webServiceClientPolicy = new Policy();

        // Specify that the policy uses the UsernameOverTransportAssertion turnkey security assertion.
        webServiceClientPolicy.Assertions.Add(new UsernameOverTransportAssertion());

        // Apply the policy to the SOAP message exchange.
        service.SetPolicy(webServiceClientPolicy);

        service.SetClientCredential(token);
    }
    #endregion
}

Next you would create an instance of this class and you could pass in your web credentials as needed. Please remember this is just an example of what I did and you may need to tweak it slightly to work with your solution but it should remain pretty similar I'd think.

Hope this helps!

ajdams