Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http Powershell System.Net.WebClient身份验证_Http_Authentication_Powershell_Webclient - Fatal编程技术网

Http Powershell System.Net.WebClient身份验证

Http Powershell System.Net.WebClient身份验证,http,authentication,powershell,webclient,Http,Authentication,Powershell,Webclient,尝试从受保护的HTTP源下载文件时使用以下代码会返回401未经授权的错误: $pfile = "\\some\local\leaf\path" $user = "MyUserName" $pwdIn = read-host -assecurestring | convertfrom-securestring | out-file $pfile $pwdOut = get-content $pFile| convertto-securestring $url="http://path.to/f

尝试从受保护的HTTP源下载文件时使用以下代码会返回401未经授权的错误:

$pfile = "\\some\local\leaf\path"
$user = "MyUserName"

$pwdIn = read-host -assecurestring | convertfrom-securestring | out-file $pfile
$pwdOut = get-content $pFile| convertto-securestring

$url="http://path.to/file"
$file = "\\localpath\to\file"
$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($user, $pwdOut)
$webclient.DownloadFile($url,$file)
跳过写入密码文件的步骤也是如此:

$pwd = read-host -assecurestring 
...
$webclient.Credentials = new-object System.Net.NetworkCredential($user, $pwd)
但是,跳过securestring创建工作正常:

 $pwd = read-host
 ...
 $webclient.Credentials = new-object System.Net.NetworkCredential($user, $pwd)
上述代码不返回任何其他错误。在顶部测试原始代码版本时,我验证了目标密码文件的存在性和明显的结构有效性。整个代码都在单个脚本中,因此convertto和convertfrom在执行期间具有相同的环境。我在x86和64位powershell中都试过

事实上,我甚至可以将安全字符串转换回正确的基本字符串,并与WebClient一起使用:

$pwdtmp = get-content $pFile| convertto-securestring
$pwdOut = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwdtmp))
$webclient.Credentials = new-object System.Net.NetworkCredential($user, $pwdOut)
因此,问题一定是在System.Net.NetworkCredential类中使用SecureString,但是,我发现的一些示例表明SecureString应该用作第二个参数

在写这篇文章时,我实际上找到了一个解决方案:

$user = "user"
$pwd = read-host -assecurestring
$creds = New-Object System.Management.Automation.PSCredential `
     -ArgumentList $user, $pwd  
$webclient.Credentials = new-object System.Net.NetworkCredential($user, $Creds.GetNetworkCredential().Password)
$webclient.DownloadFile($url,$file)
但没有解释为什么SecureString类型不起作用,而PSCredential.Password起作用。执行$creds | gm清楚地表明$creds.Password实际上也是一个SecureString:

NetworkCredential(
)
NetworkCredential(
    String userName,
    String password,
)
NetworkCredential(
    String userName,
    String password,
    String domain,
)
类型名称:System.Management.Automation.PSCredential

名称成员类型定义

-----------------------Equals方法bool Equals(System.Object obj)
GetHashCode方法int GetHashCode()
GetNetworkCredential方法System.Net.NetworkCredential GetNetworkCredential()GetType方法类型GetType() ToString方法字符串ToString()
密码属性System.Security.SecureString密码 {get;}


提供了什么?

您可能正在使用早期版本的.Net Framework中的System.Net.NetworkCredential。接受SecureString的构造函数重载是在.Net 4.0中引入的。如果将SecureString传递给接受字符串的构造函数,powershell会将SecureString转换为字符串,从而将该字符串的值呈现为“System.Security.SecureString”。您可以测试一些东西:

  • 调用构造函数,然后查看其密码属性以查看值。如果我的假设是正确的,它将是“System.Security.SecureString”

  • 查看您正在使用的.Net Framework的版本:

    $PSVersionTable.CLRVersion

  • 查询对象以查看其具有哪些构造函数:

    a。从Jeffrey Snover复制并粘贴此文件

    b。调用函数:

    获取构造函数system.net.networkcredential

  • 从下面的输出(我使用的是.net 2.0)可以看出,没有接受SecureString的重载:

    NetworkCredential(
    )
    NetworkCredential(
        String userName,
        String password,
    )
    NetworkCredential(
        String userName,
        String password,
        String domain,
    )
    

    我猜$Creds.GetNetworkCredential()。密码之所以有效,是因为它返回一个普通字符串。那么为什么安全字符串不起作用呢?谢谢你的回答!我确实在这个环境中运行.NET2。