1

I have written a test application to validate some user details against an internal RESTful service.

If I make the request directly through a browser I get a response from the service BUT if I make the request via my code, the response states that I need proxy server authentication.

Whilst I could provide user configurable settings so that the proxy parameters could be passed it just feels wrong and I'm not sure if my code is incorrect.

Below is the code snippet that fails, followed by the snippet with the proxy details.

/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
    bool valid = false;

    try
    {
        // Create the XML to be passed as the request
        XElement root = BuildRequestXML("LOGON");

        // Add the action to the service address
        Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");

        // Make the RESTful request to the service using a POST
        using (HttpResponseMessage response = new HttpClient().Post(serviceReq, HttpContentExtensions.CreateDataContract(itrent)))
        {
            // Retrieve the response for processing
            response.Content.LoadIntoBuffer();
            string returned = response.Content.ReadAsString();

            // TODO: parse the response string for the required data

        }
    }
    catch (Exception ex)
    {
        Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
        valid = false;
    }

    return valid;
}

Now for the chunk that works but has issues...

/// <summary>
/// Validate the user details
/// </summary>
/// <returns>True if the user credentials are valid, else false</returns>
public bool ValidateUser()
{
    bool valid = false;

    try
    {
        // Create the XML to be passed as the request
        XElement root = BuildRequestXML("LOGON");

        // Add the action to the service address
        Uri serviceReq = new Uri(m_ServiceAddress + "?obj=LOGON");

        // Create the client for the request
        using (HttpClient client = new HttpClient())
        {

            // Create a proxy to get around the network issues and assign it to the http client
            WebProxy px = new WebProxy( <Proxy server address>, <Proxy Server Port> );
            px.Credentials = new NetworkCredential( <User Name>, <Password>, <Domain> );
            client.TransportSettings.Proxy = px;

            // Mare the RESTful request
            HttpResponseMessage response = client.Post(serviceReq, HttpContentExtensions.CreateDataContract(root));

            // Retrieve the response for processing
            response.Content.LoadIntoBuffer();
            string returned = response.Content.ReadAsString();

            // TODO: parse the response string for the required data

        }
    }
    catch (Exception ex)
    {
        Log.WriteLine(Category.Serious, "Unable to validate the User details", ex);
        valid = false;
    }

    return valid;
}

All suggestions are gratefully received. Cheers.

TeamWild
  • 2,460
  • 8
  • 43
  • 53

2 Answers2

3

When you browse to the service you are using the proxy settings for the signed on user. When you try to go through a proxy through the web service, it is probably running as the ASP.NET account which does not have proxy settings configured.

You can either

  • Provide configurable proxy settings as you suggest
  • Run the web service under an account that is configured with the correct proxy settings (using Windows Authentication perhaps?)
  • Try adding section to your web.config <system.net> <defaultProxy useDefaultCredentials="true"> <proxy usesystemdefault="true"/> </defaultProxy> </system.net>

Good discussion on it here

Community
  • 1
  • 1
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • @xelibrion I've marked this as the answer as the other options may be helpful to others who have the same problem but thanks all the same. – TeamWild May 04 '11 at 13:18
0

Put this code to your app.config. It should disable proxy

<system.net> 
  <defaultProxy 
    enabled="false" 
    useDefaultCredentials="false" > 
    <proxy/> 
    <bypasslist/> 
    <module/> 
  </defaultProxy> 
</system.net> 
xelibrion
  • 2,220
  • 1
  • 19
  • 24