0

Works great from my local machine hitting the remote service. However, when I deploy a console app and attempt to run as a scheduled task, I'm getting a 401 on this call. Possibly related to: Unable to authenticate to ASP.NET Web Api service with HttpClient

The endpoint is using https. When I capture the call in fiddler, it says:

No Proxy-Authorization Header is present.
No Authorization Header is present.

Here's the response headers in the 401:

HTTP/1.1 401 Unauthorized
Content-Type: text/html
Server: Microsoft-IIS/7.5
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
Date: Wed, 02 Sep 2015 19:19:25 GMT
Content-Length: 1293
Proxy-Support: Session-Based-Authentication

Here's the client code:

public async Task<ApiResult<string>> GetStuff(int id)
{
    var queryBuilder = new UriBuilder(BaseUrl);
    queryBuilder.Path += string.Format("/Stuff/{0}", id);

    using (var client = new HttpClient(new HttpClientHandler()
    {
        UseDefaultCredentials = true,
        PreAuthenticate = true,
    }))
    {
        client.BaseAddress = new Uri(BaseUrl);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var response = await client.GetAsync(queryBuilder.Uri.ToString());
        if (response.IsSuccessStatusCode)
        {
            var data = await response.Content.ReadAsAsync<string>();
            if (data == null)
            {
                return new ApiResult<string>
                {
                    Success = false,
                    Message = string.Format("No stuff found for id.  id={0}", id),
                    Value = data
                };
            }
            return new ApiResult<string>
            {
                Success = true,
                Message = "",
                Value = data
            };
        }
        else
        {
            var message = string.Format("Could not get stuff by id. id={0}, reasonPhrase={1},  ", id, response.ReasonPhrase);
            return new ApiResult<ShippingPreferences>
            {
                Success = false,
                Message = message,
            };
        }
    }
}
Community
  • 1
  • 1
BDig
  • 93
  • 7
  • I assume this is also related: [How to ignore the certificate check when ssl](http://stackoverflow.com/q/12506575/1316573), but can't be sure. – Daniel Sep 02 '15 at 19:59
  • How are you running it as a scheduled task? – 1.618 Sep 02 '15 at 20:00
  • 1
    If you're using the Windows Task Scheduler service, then it's probably running as Local System. Does the "SYSTEM" user have access to the directory that the service is deployed to? – 1.618 Sep 02 '15 at 20:05
  • Just a regular windows task. I'll have it run overnight. Eventually, I'll have the task run under a service account. Currently however, I have it using my credentials which have permission on the service. I can verify by hitting the GET url from my web browser. – BDig Sep 02 '15 at 20:59
  • Ran the scheduled task from a different server and it works. It must be something about that system. I did notice that the .NET version on the server that fails is running 4.5.1 and 4.5 on the one that worked. I'm targeting 4.5 on my app. I would assume 4.5.1 should run a 4.5 assembly without issues. – BDig Sep 02 '15 at 22:28

0 Answers0