简体   繁体   English

如何在C#自定义Cmdlet参数中屏蔽用户输入密码

[英]How to mask user input password in parameter of C# custom Cmdlet

In my program I need to ask user for password in parameter: 在我的程序中,我需要在参数中要求用户输入密码:

[Cmdlet(VerbsCommon.Get, "MyTest"]
public class GetMyTest : PSCmdlet
{
    [Parameter(Mandatory=true)]
    public ? Password { get; set;}
}

I can't figure out what is the correct type to use for Password. 我不知道密码使用的正确类型是什么。 In another question: How does one securely handle passwords in a custom written PowerShell cmdlet? 另一个问题是: 如何在自定义的书面PowerShell cmdlet中安全地处理密码?

The accepted answer asked to use read-host in the code, but I have to use parameter to ask for this field. 可接受的答案要求在代码中使用读取主机,但是我必须使用参数来询问此字段。

I also tried to use SecureString for Password, but I am not able to give a SecureString to this parameter as it will be automatically accepted as string, not secure string. 我也尝试将SecureString用于密码,但是我无法为该参数提供SecureString,因为它将被自动接受为字符串,而不是安全字符串。

Is it any way to achieve the following usage: 是否可以实现以下用法:

Get-MyTest -Password ***** (where I actually type in 'abcde' but the input is masked.)

Usually what is done is that you store the password in a string-typed variable and then you hash it somehow (SHA256, MD5, or so) to store it in the database (or to compare it and allow or not user to access the application. 通常要做的是将密码存储在字符串类型的变量中,然后以某种方式对其进行哈希处理(SHA256,MD5等)以将其存储在数据库中(或进行比较以允许或不允许用户访问应用程序) 。

So, I am not sure there is a better type for that. 因此,我不确定是否有更好的类型。

There is no way to make particular portion of command line input to be protected with asterisks - command line input is just plain text. 无法使用星号来保护命令行输入的特定部分-命令行输入只是纯文本。

Options 选项

  • store password in a file so it is not visible in the input. 将密码存储在文件中,以便在输入中不可见。 The question now is how to protect that file but may work for some cases - ie specify encrypted password similar to encrypted settings in web.config 现在的问题是如何保护该文件,但在某些情况下可能会起作用-即,指定类似于web.config中的加密设置的加密密码
  • require user to type password in 要求用户输入密码
  • instead password require user to log in with that account (for Windows auth) 相反,密码要求用户使用该帐户登录(对于Windows身份验证)
  • if there is some way to get short-lived token (ie using OAUTH) - you may use that in clear text instead of password 如果有某种方法可以获取短暂的令牌(即使用OAUTH),则可以明文形式而不是密码来使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM