0

I am trying to consume WCF webserice which is siteminder protected. The issue is when I am trying to browse the webservice URL in browser it is working fine with the credential that I have supplied.

But when I am trying to do the same programmatically, it's throwing an error - error #401 unauthorized.

for reference - http://www.codeproject.com/Articles/80314/How-to-Connect-to-a-SiteMinder-Protected-Resource

        CookieContainer cookies = null;
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        string responseString = null;
        NameValueCollection tags = null;
        string url = null;
        url = PROTECTED_URL;
        Debug.WriteLine("Step 1: Requesting page @" + url);
        request = (HttpWebRequest)WebRequest.Create(url);
        request.AllowAutoRedirect = false;
        response = (HttpWebResponse)request.GetResponse();
        ShowResponse(response);
        // Step 2: Get the redirection location
        // make sure we have a valid response
        if (response.StatusCode != HttpStatusCode.Found)
        {
            throw new ApplicationException();
        }
        url = response.Headers["Location"];
        // Step 3: Open a connection to the redirect and load the login form, 
        // from this screen we will capture the required form fields.
        Debug.WriteLine("Step 3: Requesting page @" + url);
        request = (HttpWebRequest)WebRequest.Create(url);
        request.AllowAutoRedirect = false;

        try
        {
            response = (HttpWebResponse)request.GetResponse();
        }
        catch (Exception ex)
        {
            string str = ex.Message.ToString();
        }
Code's
  • 208
  • 2
  • 18

1 Answers1

0

It's my HtTpClient to call WCF.

 public async Task<string> webClient(string method, string uri)
        {
                try
                {

                    var client = new HttpClient();
                    client.Timeout =new TimeSpan(0,0,0,10);
                    client.BaseAddress = new Uri(uri);
                    client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                    var response = client.GetAsync(method).Result;

                    string content = await response.Content.ReadAsStringAsync();
                    return content;

                }
                catch (Exception ex)
                {
                    return "Error";
                }
}

Uri is base adress, method is your method name.

string response = webClient(uri + "/GetSomething/", uri).Result;
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28