与域外的用户访问网络上的文件
Accessing file on network with user outside of the domain
我需要与可能不在域中的用户一起访问网络驱动器中的文件。
我当前的代码是:
private async Task GetUxVersionsFromServer()
{
string path = @$"\{IpAddress}\...\...\...";
if(!await GetFiles(path))
{
using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
{
bool retry = true;
do
{
(var ok, var username, var password) = _dialogService.ShowPasswordInput();
if (ok)
{
if (unc.NetUseWithCredentials(path, username, "domain", password))
{
await GetFiles(path);
retry = false;
}
}
else
{
retry = false;
}
} while (retry);
}
}
}
private async Task<bool> GetFiles(string path)
{
try
{
var zipFiles = await Task.FromResult(System.IO.Directory.GetFiles(path, "VERSION*.zip"));
Versions = new ObservableCollection<string>(zipFiles);
return true;
}
catch (IOException)
{
return false;
}
}
我使用 class UNCAccessWithCredential
来自 here
它工作正常。
如果用户有权访问该目录,则不应出现密码条目。
唯一的问题是我无法测试 Windows 用户是否可以在不捕获异常的情况下访问该目录。
有没有办法查询登录的 Windows 用户是否有权访问网络目录?
有没有办法查询登录的 Windows 用户是否在域中?
这里有很多方法可以确定目录权限:How do you check for permissions to write to a directory or file?
至于域成员,使用这个:https://docs.microsoft.com/en-us/dotnet/api/system.environment.userdomainname?view=netframework-4.8
The UserDomainName property first attempts to get the domain name component of the Windows account name for the current user. If that attempt fails, this property attempts to get the domain name associated with the user name provided by the UserName property. If that attempt fails because the host computer is not joined to a domain, then the host computer name is returned.
最后我是这样解决的:
private async Task GetUxVersionsFromServer()
{
string path = @$"\{server}\...";
if (Environment.UserDomainName.ToLower() != "myDomain")
{
bool success = false;
bool ok;
do
{
(bool result, var username, var password) = _dialogService.ShowPasswordInput();
ok = result;
if (ok)
{
try
{
using (new NetworkConnection(path, new NetworkCredential($@"myDomain\{username}", password)))
{
success = await GetFiles(path);
}
}
catch (System.ComponentModel.Win32Exception ex)
{
success = false;
}
}
} while (!success && ok);
if(!ok)
{
int test = 0;
}
}
else
{
await GetFiles(path);
}
}
我从 here
拿走了 class NetworkConnection
我需要与可能不在域中的用户一起访问网络驱动器中的文件。
我当前的代码是:
private async Task GetUxVersionsFromServer()
{
string path = @$"\{IpAddress}\...\...\...";
if(!await GetFiles(path))
{
using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
{
bool retry = true;
do
{
(var ok, var username, var password) = _dialogService.ShowPasswordInput();
if (ok)
{
if (unc.NetUseWithCredentials(path, username, "domain", password))
{
await GetFiles(path);
retry = false;
}
}
else
{
retry = false;
}
} while (retry);
}
}
}
private async Task<bool> GetFiles(string path)
{
try
{
var zipFiles = await Task.FromResult(System.IO.Directory.GetFiles(path, "VERSION*.zip"));
Versions = new ObservableCollection<string>(zipFiles);
return true;
}
catch (IOException)
{
return false;
}
}
我使用 class UNCAccessWithCredential
来自 here
它工作正常。
如果用户有权访问该目录,则不应出现密码条目。 唯一的问题是我无法测试 Windows 用户是否可以在不捕获异常的情况下访问该目录。
有没有办法查询登录的 Windows 用户是否有权访问网络目录?
有没有办法查询登录的 Windows 用户是否在域中?
这里有很多方法可以确定目录权限:How do you check for permissions to write to a directory or file?
至于域成员,使用这个:https://docs.microsoft.com/en-us/dotnet/api/system.environment.userdomainname?view=netframework-4.8
The UserDomainName property first attempts to get the domain name component of the Windows account name for the current user. If that attempt fails, this property attempts to get the domain name associated with the user name provided by the UserName property. If that attempt fails because the host computer is not joined to a domain, then the host computer name is returned.
最后我是这样解决的:
private async Task GetUxVersionsFromServer()
{
string path = @$"\{server}\...";
if (Environment.UserDomainName.ToLower() != "myDomain")
{
bool success = false;
bool ok;
do
{
(bool result, var username, var password) = _dialogService.ShowPasswordInput();
ok = result;
if (ok)
{
try
{
using (new NetworkConnection(path, new NetworkCredential($@"myDomain\{username}", password)))
{
success = await GetFiles(path);
}
}
catch (System.ComponentModel.Win32Exception ex)
{
success = false;
}
}
} while (!success && ok);
if(!ok)
{
int test = 0;
}
}
else
{
await GetFiles(path);
}
}
我从 here
拿走了 classNetworkConnection