我有一个网络共享驱动器(“\ serveur \文件夹”),我想在其上复制文件。 我可以在驱动器上写入特定用户(“用户”/“通行证”)。 如何使用C#访问共享带写权限的共享?在网络共享驱动器上复制文件
15
A
回答
23
未经测试的代码,但它会类同:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
// http://pinvoke.net/default.aspx/advapi32/LogonUser.html
IntPtr token;
LogonUser("username", "domain", "password", LogonType.LOGON32_LOGON_BATCH, LogonProvider.LOGON32_PROVIDER_DEFAULT);
WindowsIdentity identity = new WindowsIdentity(token);
WindowsImpersonationContext context = identity.Impersonate();
try
{
File.Copy(@"c:\temp\MyFile.txt", @"\\server\folder\Myfile.txt", true);
}
finally
{
context.Undo();
}
0
创建一个有权写入网络驱动器的用户,并使用c#中的模拟用户访问驱动器时使用该用户。
1
下面是ASP.NET应用程序的工作示例。 Original source
private void SendToFileShare(byte[] pdfData, string fileName)
{
if(pdfData == null)
{
throw new ArgumentNullException("pdfData");
}
if (string.IsNullOrWhiteSpace(fileName))
{
//Assign a unique name because the programmer failed to specify one.
fileName = Guid.NewGuid().ToString();
}
else
{
//Should probably replace special characters (windows filenames) with something.
}
string networkShareLocation = @"\\your\network\share\";
var path = $"{networkShareLocation}{fileName}.pdf";
//Credentials for the account that has write-access. Probably best to store these in a web.config file.
var domain = "AB";
var userID = "Mr";
var password = "C";
if (ImpersonateUser(domain, userID, password) == true)
{
//write the PDF to the share:
System.IO.File.WriteAllBytes(path, report);
undoImpersonation();
}
else
{
//Could not authenticate account. Something is up.
//Log or something.
}
}
/// <summary>
/// Impersonates the given user during the session.
/// </summary>
/// <param name="domain">The domain.</param>
/// <param name="userName">Name of the user.</param>
/// <param name="password">The password.</param>
/// <returns></returns>
private bool ImpersonateUser(string domain, string userName, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}
/// <summary>
/// Undoes the current impersonation.
/// </summary>
private void undoImpersonation()
{
impersonationContext.Undo();
}
#region Impersionation global variables
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
#endregion
相关问题
- 1. 如何在共享网络驱动器上存储文件c#
- 2. ipad:读取共享网络驱动器上的文件
- 3. 检查文件是否是共享网络驱动器上accessable
- 4. 在网络共享上移动文件
- 5. 访问共享网络驱动器
- 6. FileSystemWatcher.Path网络共享驱动器异常
- 7. 检查共享网络驱动器中是否存在文件
- 8. 共享文件夹/网络驱动器服务器2008
- 9. 删除从网络文件共享驱动器
- 10. 从网络共享驱动器远程执行exe文件?
- 11. 复制文件到共享驱动器在ASP.net失败c#
- 12. 从网络共享驱动器复制文件到桌面使用DOS中的DOS复制命令
- 13. 使用C#将文件从本地驱动器复制到共享驱动器
- 14. 在网络上重新共享文件
- 15. Android上传到网络驱动器(samba共享)性能问题
- 16. 使用SharePoint 2007连接到网络上的共享驱动器
- 17. 在工作的共享网络驱动器上使用版本控制系统
- 18. 使用PowerShell将文件复制到共享驱动器
- 19. 复制本地共享文件,将Linux本地驱动器
- 20. 网络驱动器上访问文件
- 21. Gradle无法将工件发布到网络/共享驱动器
- 22. 保存时自动将文件复制到网络驱动器
- 23. Shuttil Python将文件复制到受限制的共享驱动器上
- 24. 复制的PDF文件共享网络和文件损坏
- 25. 通过文件共享,用户身份验证在网络上复制文件
- 26. 与.bat共享文件夹在网络上的问题(网络共享)
- 27. VB6:在网络共享内复制文件
- 28. 负载从共享网络驱动器在C#中的DLL
- 29. 可移动驱动器的网络共享消失(Windows)
- 30. 将网络文件复制到驱动器
其中是'LogonUser'函数的声明? – 2018-01-30 13:35:16
@Nitin:请参阅代码中的评论? http://pinvoke.net/default.aspx/advapi32/LogonUser.html。这是一个win32 API调用 – 2018-01-31 00:39:46