我正在尝试模拟远程管理员用户,以便我可以对远程 Linux 机器上存在的文件进行修改。但是我收到错误消息,因为访问路径被拒绝。然而,这件事我可以通过 putty 使用命令手动完成:
sudo -S -u wtsnqa rm /path-to-file/
任何帮助都是值得的。
我的代码:
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "sj1slm612",
UserName = "userid",
Password = "password",
SshHostKeyFingerprint = "ssh-rsa 2048 fa:e9:58:24:1b:41:a3:15:63:0d:c0:72:41:5d:51:7a"
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Performing removing files from remote server via impersonation.......
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
using (WindowsIdentity Authorized_user = new WindowsIdentity("wtsnqa"))
{
using (WindowsImpersonationContext context = Authorized_user.Impersonate())
{
File.Delete(@"\\sj1slm612\apps\instances\express_13000\configuration\standalone-full.xml");
File.Delete(@"\\sj1slm612\apps\instances\query_13100\configuration\standalone-full.xml");
File.Delete(@"\\sj1slm612\apps\instances\wppapi_13200\configuration\standalone-full.xml");
File.Delete(@"\\sj1slm612\apps\instances\wppgui_13300\configuration\standalone-full.xml");
Console.WriteLine("All config files removed from sj1slm612");
Console.ReadLine();
context.Undo();
}