Allow console application to access Windows Authenticated web app

6.8k Views Asked by At

I have a MVC web application which uses Windows Authentication. So we do things like System.Web.HttpContext.Current.User.Identity.Name

Now there is a console app I'm writing that would be run as part of a batch job. All this console app needs to do is call an ActionResult in the main web app which would then carry out some maintenance tasks.

Now, since the console app doesn't have an identity within the domain controller/Active Directory, the call using System.Net.WebClient().DownloadString() keeps throwing a 401 error.

My question is, is there a way to allow this application to call this link in the main web app without having to disable WindowsAuthentication in the web.config/IIS?

Any ideas/suggestions welcome.

All the console app contains is

static void Main(string[] args)
        {
            try
            {
                new System.Net.WebClient().DownloadString(System.Configuration.ConfigurationSettings.AppSettings["http://foo.com/"]);
            }
            catch
            {

            }
        }
1

There are 1 best solutions below

1
On BEST ANSWER

Use UseDefaultCredentials:

        var client = new WebClient
                     {
                         UseDefaultCredentials = true
                     };
        client.DownloadString(address);

this will use whatever the console app is running as (which can be specified on the Scheduled Task settings).