1

I have run into a snag while building a web client that makes use of a RESTful web service using Integrated Windows Authentication.

When I run the client locally (as myself), it works fine, and I see my user id in the IIS logs. But, when I run it remotely, no credentials are passed (i.e. I do not see my user id in the IIS logs), and I get an error that

"The remote server returned an error: (401) Unauthorized."

Both the local and published client call the same service. Also note that the IIS logs show two entries (one without a username and one with a username) when I run it locally. When I run in from the published location, there are still two entries, but neither shows the username.

Below is the code I am using to make the call:

var request = (HttpWebRequest) WebRequest.Create(ConfigurationManager.AppSettings["positionServicePath"] + "?positionNumberSearch=" + searchString);
        request.Method = "GET";
        request.ContentType = "application/json";
        request.UseDefaultCredentials = true;
        request.PreAuthenticate = false;

        CredentialCache cc = new CredentialCache();
        cc.Add(
            new Uri(ConfigurationManager.AppSettings["positionServicePath"]),
            "Negotiate",
            CredentialCache.DefaultNetworkCredentials);
        request.Credentials = cc;

        // Get the Response
        using (var response = request.GetResponse()){
            var reader = new StreamReader(response.GetResponseStream());

            // Convert the response to objects from Json
            var resultList = JsonConvert.DeserializeObject<IEnumerable<PositionModel>>(reader.ReadToEnd());

            // Return an empty list, if no data was retrieved
            return resultList != null ? resultList.ToList() : new List<PositionModel>();
        }

Any help would be greatly appreciated.

  • The only way I have been able to get any results is by explicitly passing in a username and password using Basic authentication. Is this truly the only way to pass credentials to an ASP.NET WebApi service? – Goofy Coder May 23 '18 at 22:34

2 Answers2

0

I think you have to create IIS user permitted to your project.

Follow this Anonymous Access

Second Option: Try to enabled anonymous access in Authentication Methods in Microsoft Internet Information Services (IIS).

Step:
1. Open your IIS.
2. In IIS Group:
3. open or select Authentication:
4. Select Anonymous Authentication:
5. On the Left Side select Enabled.

Good Luck!

Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
  • I have actually set up permissions in IIS on the web service to allow the service account being used to run the AppPool for the consuming site. But, it doesn't work. – Goofy Coder May 23 '18 at 22:33
  • Additionally, we don't want to allow Anonymous authentication, because the service returns personnel data. – Goofy Coder May 24 '18 at 16:42
0

I found the answer to this issue was the same for the answer to the following Stack Overflow post: Unable to authenticate to ASP.NET Web Api service with HttpClient.

Thank you to those who took the time to look this over!