Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow console application to access Windows Authenticated web app

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
            {

            }
        }
like image 576
reggaemahn Avatar asked Oct 28 '25 13:10

reggaemahn


1 Answers

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).

like image 148
freedomn-m Avatar answered Oct 31 '25 04:10

freedomn-m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!