注意:我不能在这里使用 PowerShell V3.0,否则我会使用 Invoke-WebRequest 并过上幸福的生活。
我有一个 PowerShell V2.0 脚本,它需要将数据发布到经过 HTTP-Basic 身份验证的资源。出于脚本的目的,我不想或不需要知道用户的密码,我只想将 PSCredentials 对象(从 PromptForCredential 返回)转换为 NetworkCredential 以与 HttpWebRequest 一起使用。
$uri = "https://example.com/some/resource/"
# Get our user's credentials...
$defaultUsername = "Some Username"
$caption = "Authentication required"
$message = "A username and password is required for ${uri}"
#$target = $uri #<<--NOTE: This prepends $uri+"\" to the username.
#$target = "" #<<--NOTE: This prepends "\" to the username.
$target = $null #<<--NOTE: This still prepends "\" to the username.
$psCredential = $Host.UI.PromptForCredential($caption, $message, $defaultUsername, $target)
# Construct a CredentialCache for HttpWebRequest...
# NOTE: We need to delete the "domain part" of the username from the PSCrential.Username, otherwise we get "Something\Username"
$username = ($psCredential.Username).Split('\')[1]
$networkCredential = New-Object System.Net.NetworkCredential($username, [System.Security.SecureString]$psCredential.Password)
$credentialCache = New-Object System.Net.CredentialCache
$credentialCache.Add( (New-Object Uri($uri)), "Basic", $networkCredential)
#...
$request = New-Object System.Net.HttpWebRequest($uri)
$request.Credentials = $credentialCache
#...
[System.Net.HttpWebResponse]$response = [System.Net.HttpWebResponse]$request.GetResponse()
这当然会失败,但有以下例外:
Exception calling "GetResponse" with "0" argument(s):
"The remote server returned an error: (401) Unauthorized."
据称我们有一个 NetworkCredential(String userName, SecureString password) 构造函数,但用户的凭据以用户名:System.Security.SecureString 的形式到达服务器。
我缺少一些小细节吗?我是否需要解密 SecureString 并将其传递给 NetworkCredential(String userName, String password) 构造函数?