gpt4 book ai didi

c# - 使用 ImpersonateLoggedOnUser 移动文件

转载 作者:行者123 更新时间:2023-11-30 16:33:40 30 4
gpt4 key购买 nike

我正在尝试移动文件,但出现此错误:

System.UnauthorizedAccessException: Access to the path is denied..
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.FileInfo.MoveTo(String destFileName)

要移动文件,我有这段代码:

public void MssFile_Move (string ssPath, string ssDestinationDirectoryPath, string ssDomain, string ssUsername, string ssPassword, out string ssError_message) {
IntPtr admin_token = IntPtr.Zero;
ssError_message = "";

try
{
DoImpersonateLoggedOnUser( ssDomain
, ssUsername
, ssPassword
, out ssError_message
, out admin_token);


FileInfo fi = new FileInfo(ssPath);
//Destination Directory does not exist ?
if ( !Directory.Exists(Path.GetDirectoryName(ssDestinationDirectoryPath)))
Directory.CreateDirectory(Path.GetDirectoryName(
ssDestinationDirectoryPath));
fi.MoveTo (ssDestinationDirectoryPath);

DoRevertToSelf(ssDomain);

}
catch (System.Exception se)
{
int ret = Marshal.GetLastWin32Error();
ssError_message += "Win32Error: " + ret + "\n";
ssError_message += se.ToString();
}
finally
{
if (admin_token != IntPtr.Zero)
CloseHandle(admin_token);
}
}

要模拟我有:

[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(string lpszUsername, string lpszDomain,
string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //handle to token for logged-on user


public void DoImpersonateLoggedOnUser ( string ssDomain
, string ssUsername
, string ssPassword
, out string ssError_message
, out IntPtr admin_token)
{
IntPtr phToken = IntPtr.Zero;
admin_token = IntPtr.Zero;
ssError_message = "";

if (ssDomain != "")
{
if (LogonUser(ssUsername, ssDomain, ssPassword, 9, 0, out phToken) != 0)
{
ImpersonateLoggedOnUser(phToken);
}
else
{
int nErrorCode = Marshal.GetLastWin32Error();

ssError_message = "Operation Failed, error: " + nErrorCode;
}
admin_token = phToken;
}
}

如果我将文件夹/文件设置为每个人的权限,它就可以工作,但我不希望这样。我做错了什么?

最佳答案

使用这段代码成功完成。

[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken);

[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //handle to token for logged-on user

[DllImport("advapi32.DLL")]
public static extern bool RevertToSelf();

[DllImport("kernel32.dll")]
public extern static bool CloseHandle(IntPtr hToken);

enum LogonType
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
NetworkClearText = 8,
NewCredentials = 9
}

enum LogonProvider
{
Default = 0,
WinNT35 = 1,
WinNT40 = 2,
WinNT50 = 3
}

int valid = LogonUser(
ssUsername,
ssDomain,
ssPassword,
(int)LogonType.Interactive,
(int)LogonProvider.WinNT50,
out admin_token);

if (valid != 0)
{
using (WindowsImpersonationContext context = WindowsIdentity.Impersonate(admin_token))
{
CloseHandle(admin_token);
FileInfo fi = new FileInfo(ssPath);

//Destination Directory does not exist ?
if (!Directory.Exists(Path.GetDirectoryName(ssDestinationDirectoryPath)))
Directory.CreateDirectory(Path.GetDirectoryName(ssDestinationDirectoryPath));

fi.CopyTo(ssDestinationDirectoryPath);
fi.Delete();
}
}

关于c# - 使用 ImpersonateLoggedOnUser 移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2950106/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com