复制网络共享驱动器上的文件
我有一个网络共享驱动器(“\ serveur \ folder”),我想复制文件。 我可以使用特定用户(“user”/“pass”)在驱动器上写入。 如何使用C#访问共享驱动写入权限?
未经测试的代码,但它将类似于:
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(); }
这是ASP.NET应用程序的一个工作示例。 原始来源
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. } } /// /// Impersonates the given user during the session. /// /// The domain. /// Name of the user. /// The password. /// 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; } /// /// Undoes the current impersonation. /// 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
创建具有写入网络驱动器权限的用户,并在c#中使用模拟以在访问驱动器时使用该用户。
- 在WCF中,服务器没有访问客户端的公钥,客户端没有访问服务器的公钥 – 仍然是Web方法操作成功。 为什么?
- c#检查一个组的用户成员?
- 在Office365上使用默认SMTP凭据的例外 – 客户端未经过身份validation,无法在MAIL FROM期间发送匿名邮件
- 从ASP.NET Core 1.1 MVC迁移到2.0后,自定义cookie身份validation无法正常工作
- 使用.NET Core的Firebase身份validation(JWT)
- OWIN身份validation,使当前令牌失效并删除cookie
- SSL / TLS中的相互身份validation
- mvc 5检查用户角色
- 从登录和注销获得通知