Search code examples
c#wpfwebclientweb-client

WPF C# - Standard (non - privilege) user not able to accss the webclient?


Here is the code I am using to check the internet connection. But it always fails. This runs fine with my admin user but not working for standard (non-privilege) user I made. What could be missing here..

if (!App.IsInternetConnected)
            {
                await Task.Factory.StartNew(() =>
                {
                    App.IsInternetConnected = Utils.IsInternetConnected();
                });
            }

            if (!App.IsInternetConnected)
            {
                App.ShowMessage("FeatureNotAvailable");
                LoginProgress.Visibility = System.Windows.Visibility.Hidden;
                return;
            }


 IsInternetConnected method is as below.

internal static bool IsInternetConnected()
    {
       try
        {
            WebClient webclient = new  WebClientPool().GetIdleWebClientObject();

            try
            {
            webclient.DownloadString("http://www.google.com");
            return true;
        }

    }
    catch (Exception)
    {
            return false;
    }
  }

GetIdleWebClientObject is as below.

 [MethodImpl(MethodImplOptions.Synchronized)]
        public WebClient GetIdleWebClientObject()
        {
            foreach (WebClient item in WebClientItems)
            {
                if (!item.IsBusy )
                {
                    Log.Instance.WriteLine("reused webclient");
                    item.Headers.Clear();
                    return item;
                }
            }
            Log.Instance.WriteLine("new webclient");
            WebClient NewItem = new WebClient();
            NewItem.UseDefaultCredentials = true;
            //NewItem.Credentials = CredentialCache.DefaultNetworkCredentials;

            NewItem.Proxy = WebRequest.GetSystemWebProxy();
            WebClientItems.Add(NewItem);
            return NewItem;
        }

Solution

  • My issue was bit out of way but may help anyone. It was in line this Log.Instance.WriteLine("new webclient"); This line was creating and writing log file in C:\ drive. Which was not allowed to standard user and throwing exception. which lead me to show no internet error which was not actually related.