我有一個網絡共享驅動器(「\ 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. 檢查文件是否是共享網絡驅動器上accessable
- 2. 在網絡共享上移動文件
- 3. 檢查共享網絡驅動器中是否存在文件
- 4. 從網絡共享驅動器遠程執行exe文件?
- 5. 使用C#將文件從本地驅動器複製到共享驅動器
- 6. 在網絡上重新共享文件
- 7. Android上傳到網絡驅動器(samba共享)性能問題
- 8. 使用SharePoint 2007連接到網絡上的共享驅動器
- 9. 在工作的共享網絡驅動器上使用版本控制系統
- 10. 使用PowerShell將文件複製到共享驅動器
- 11. 網絡驅動器上訪問文件
- 12. Gradle無法將工件發佈到網絡/共享驅動器
- 13. 複製的PDF文件共享網絡和文件損壞
- 14. 通過文件共享,用戶身份驗證在網絡上覆制文件
- 15. VB6:在網絡共享內複製文件
- 16. 可移動驅動器的網絡共享消失(Windows)
- 17. 將網絡文件複製到驅動器
- 18. 複製文件fo映射網絡驅動器使用FileInfo.CopyTo
- 19. 將網絡文件複製到本地驅動器
- 20. ASP.NET - 將文件複製到映射的網絡驅動器
- 21. 谷歌驅動器SDK共享文件
- 22. 在OSX上使用python將文件複製到網絡路徑或驅動器
- 23. 網絡驅動器上的IIS共享配置 - 如果網絡關閉一點,IIS不會恢復
- 24. 將文件複製到WiX網絡共享
- 25. 在網絡共享上移動文件時的性能問題
- 26. ASPx Dev Express文件上傳控制映射網絡驅動器
- 27. .NET安裝項目 - 安裝到共享網絡驅動器
- 28. 推git的更改共享網絡驅動器
- 29. 與Laravel共享網絡驅動器5.4存儲
- 30. 在網絡驅動器上共享sqlite數據庫的任何實際經驗?
其中是'LogonUser'函數的聲明? – 2018-01-30 13:35:16
@Nitin:請參閱代碼中的評論? http://pinvoke.net/default.aspx/advapi32/LogonUser.html。這是一個win32 API調用 – 2018-01-31 00:39:46