【问题标题】:Convert NetworkCredentials to a PSCredential?将 NetworkCredentials 转换为 PSCredential?
【发布时间】:2012-04-12 09:23:10
【问题描述】:

是否可以将 NetworkCredentials(或变体)转换为 PSCredential?

我想透明地使用当前用户的凭据,但不想提示他们。

【问题讨论】:

    标签: c# security authentication powershell networkcredentials


    【解决方案1】:
    [System.Reflection.Assembly]::Load("System.Management.Automation")
    $dc = [System.Net.CredentialCache]::DefaultCredentials
    $psc = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $dc.UserName, $dc.SecurePassword
    

    这样就可以了,只是没有为运行 PowerShell 的当前应用程序域设置默认凭据。

    至少这说明了如何从持久的用户名和密码创建PSCredentialSecureString 总是很糟糕,但我相信 NetworkCredential 提供了一个可以使用的纯文本构造函数和一个可供读取的 SecureString 属性。

    【讨论】:

      【解决方案2】:
      $cred = new-object PSCredential($nc.UserName, (ConvertTo-SecureString $nc.Password -AsPlainText -Force))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多