【问题标题】:How to change a users password in Linux using C#如何使用 C# 在 Linux 中更改用户密码
【发布时间】:2022-01-20 15:33:30
【问题描述】:

以下场景:

  • 我确实有 sudo 权限
  • 我知道一个用户的新旧密码

现在我想编写一个可以更改用户密码的 C# .NET 6 应用程序。 在 Linux 中我会调用

sudo passwd username

我的第一个想法是创建一个新进程,重定向标准输入并将密码写入那里。
另一种选择是创建一个 bash 脚本并从 C# 调用它(也使用新进程)。

但是,对我来说,两者都显得有点老套。是否有任何选项可以在 C# 中更改密码?

【问题讨论】:

  • 使用 SSH。 linux 机器需要启用 SSH(或使用 Telnet)。请参阅:github.com/sshnet/SSH.NET?force_isolation=true
  • 我不明白 - 为什么要使用 SSH?我的程序在我想更改用户密码的同一台机器上运行。不需要远程访问。
  • 对不起。大多数人在提出这个问题时都是通过远程连接进行的。您可以通过等效于 shell 命令的 Process 类来执行此操作。请参阅:developers.redhat.com/blog/2019/10/29/…
  • 是的,这是我在主帖中已经写过的第一个意图。但是我问是否有更好的,进程内的方法来做它,比如散列它并将它写入文件或类似的东西,例如影子文件,但我不是 Linux 专业人士所以不知道这是否可行或一个好方法
  • 使用/usr/sbin/chpasswd 可能是最简单的解决方案。

标签: c# .net linux .net-core .net-6.0


【解决方案1】:

如果您已经拥有 linux shell 脚本,那么这些内容应该可以正常工作。

   if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)){    
        var ShellScriptPath =  $"-c '{ShellScriptPath } &>> {OutputTextFilePath}'";
        var fileInfo = new FileInfo(ShellScriptPath);
        var startInfo = new ProcessStartInfo
        {
           CreateNoWindow = true,
           RedirectStandardOutput = true,
           RedirectStandardError = true,
           UseShellExecute = false,
           FileName = "/bin/bash",
           WindowStyle = ProcessWindowStyle.Hidden,
           Arguments = fileInfo.FullName,
           WorkingDirectory = WorkingDirectoryPath,
        };
        
        using Process process = Process.Start(startInfo);
        process.WaitForExit();
        process.Dispose();
    }

【讨论】:

    【解决方案2】:

    程序.cs

    class PasswordChanger
    {
      static void Main(string[] args)
      {
        string password = args[1];
        string user = args[0];
        Console.WriteLine(ShellHelper.Bash($"echo -e \"{password}\n{password}\n\" | sudo passwd {user}"));
      }
    }
    

    LinuxHelper.cs

    using System;
    using System.Diagnostics;
    
    namespace LinuxHelper
    {
      public static class ShellHelper
      {
        public static string Bash(string cmd)
        {
          string escapedArgs = cmd.Replace("\"", "\\\"");
    
          Process process = new Process()
          {
            StartInfo = new ProcessStartInfo
            {
              FileName = "/bin/bash",
              Arguments = $"-c \"{escapedArgs}\"",
              RedirectStandardOutput = true,
              UseShellExecute = false,
              CreateNoWindow = true,
            }
          };
    
          process.Start();
          string result = process.StandardOutput.ReadToEnd();
          process.WaitForExit();
    
          return result;
        }
      }
    }
    

    上面的代码有效,需要两个参数,第一个是用户名,第二个是密码。我已将解决方案分成两个文件,因为 LinuxHelper/Shellhelper 可用于您需要调用的任何 shell 命令。主要功能/方法也可以轻松修改为任何项目中的标准方法。如果您确实将 Main 方法更改为类内的方法,我建议您将 (string[] args) 更改为 (string user, string password) 然后您可以删除方法内的字符串声明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-16
      • 2012-10-22
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多