WPF C# - 标准(非特权)用户无法访问 Web 客户端?
WPF C# - Standard (non - privilege) user not able to accss the webclient?
这是我用来检查互联网连接的代码。但它总是失败。这对我的管理员用户运行良好,但不适用于我创建的标准(非特权)用户。这里可能缺少什么..
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 如下
[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;
}
我的问题有点绕口,但可能对任何人都有帮助。
这条 Log.Instance.WriteLine("new webclient");
行是在 C:\
驱动器中创建和写入日志文件。不允许标准用户和抛出异常。
这导致我没有显示实际上不相关的互联网错误。
这是我用来检查互联网连接的代码。但它总是失败。这对我的管理员用户运行良好,但不适用于我创建的标准(非特权)用户。这里可能缺少什么..
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 如下
[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;
}
我的问题有点绕口,但可能对任何人都有帮助。
这条 Log.Instance.WriteLine("new webclient");
行是在 C:\
驱动器中创建和写入日志文件。不允许标准用户和抛出异常。
这导致我没有显示实际上不相关的互联网错误。