Tôi có một ổ chia sẻ mạng ("\ serveur \ folder") mà tôi muốn sao chép tập tin. Tôi có thể viết trên ổ đĩa với một người dùng cụ thể ("người dùng"/"vượt qua"). Làm thế nào tôi có thể truy cập vào ổ đĩa được chia sẻ với đặc quyền ghi bằng C#?Sao chép tập tin trên một ổ đĩa mạng chia sẻ
15
A
Trả lời
23
đang chưa được kiểm tra, nhưng nó sẽ tương tự như:
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
Tạo người dùng có quyền ghi vào ổ đĩa mạng và sử dụng mạo danh trong C# để sử dụng người dùng đó khi truy cập vào ổ đĩa.
1
Dưới đây là một ví dụ làm việc cho các ứng dụng 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
Các vấn đề liên quan
- 1. Cách truy cập ổ đĩa mạng chia sẻ trong node.js
- 2. Sao chép tập tin qua mạng qua chia sẻ tập tin, xác thực người dùng
- 3. Chia sẻ ổ đĩa được ánh xạ trên Windows
- 4. Lập trình kiểm tra xem ký tự ổ đĩa có phải là ổ đĩa mạng/chia sẻ
- 5. Làm thế nào để làm cho sqlite mạnh mẽ trên một ổ đĩa chia sẻ Windows
- 6. File.Copy() từ một mạng chia sẻ với một phần khác trên cùng một máy có sao chép tập tin qua mạng không?
- 7. Sao chép tập tin qua kết nối mạng gián đoạn
- 8. Lập bản đồ ổ đĩa mạng mà không cần mã hóa ổ đĩa trong một tập tin batch
- 9. Sao chép tệp vào thư viện chia sẻ trong R
- 10. Không thể truy cập tệp trên chia sẻ mạng được ánh xạ ổ đĩa từ dịch vụ Windows
- 11. Sao chép thư mục sang một ổ đĩa khác
- 12. kho svn trên chia sẻ mạng Windows
- 13. Sao chép tập tin an toàn trên NAS Filer
- 14. Ngoại lệ bảo mật .NET trên mạng chia sẻ
- 15. Bản sao SVN hoạt động trên mạng Chia sẻ
- 16. Truy nhập mạng chia sẻ từ bên trong VBScript, ví dụ FileSystemObject
- 17. Liên kết ổ đĩa UNC/Mạng trên trang html
- 18. Sqlite qua mạng chia sẻ
- 19. Vista UAC - Sự cố khi ánh xạ ổ đĩa mạng
- 20. Di chuyển các tập tin trên các ổ đĩa khác nhau trong .NET
- 21. script để ánh xạ ổ đĩa mạng
- 22. Đồng thời trong một repo GIT trên một thư mục chia sẻ mạng
- 23. cách sao chép tệp vào thư mục chia sẻ capistrano
- 24. Tìm hiểu ai đang khóa tệp trên mạng chia sẻ
- 25. Sao chép Bảng tính Google + Chia sẻ với cùng một người dùng trong tập lệnh
- 26. Cảnh báo bảo mật khi mở tập tin từ mạng chia sẻ
- 27. Đọc một tập tin từ một mạng chia sẻ thời gian tạo của nó cho lần ghi cuối cùng
- 28. Git không thực hiện được việc push ban đầu cho repo mới trên ổ đĩa mạng
- 29. Hệ thống tập tin tốt cho ổ đĩa NAND được nhúng là gì?
- 30. Ghi tệp I/O vào ổ đĩa mạng dùng chung bằng thông tin
nơi khai báo chức năng 'LogonUser'? –
@Nitin: xem nhận xét trong mã? http://pinvoke.net/default.aspx/advapi32/LogonUser.html. Đó là cuộc gọi win32 api –