2011-04-11 3 views
6

클라이언트 측 인증서 인증을 요구하는 웹 페이지를 검사하지는 않을 것입니다. Certstore의 Cert를 Webrequest에 제공하려면 어떻게합니까? Proxy 내에서 Credentials odr에 이것을 지정하는 방법이 있습니까?Powershell의 WebClient에 인증서를 추가하는 방법

$webclient = New-Object Net.WebClient 
# The next 5 lines are required if your network has a proxy server 
$webclient.Credentials = [System.Net.CredentialCache]::DefaultCredentials 
if($webclient.Proxy -ne $null)  { 
    $webclient.Proxy.Credentials = ` 
      [System.Net.CredentialCache]::DefaultNetworkCredentials 
} 
# This is the main call 
$output = $webclient.DownloadString("$URL") 

추신 : 어쩌면이 도움이 : 당신이 다음 사용할 수 How can you add a Certificate to WebClient (C#)? 그러나 나는 그것을 얻지 않는다 ... ;-)

+0

그게 무슨 뜻입니까? HttpWebRequest를 직접 사용하거나 인증서를 추가 할 수 있도록 WebClient를 재정의해야한다는 것입니다. – JasonMArcher

답변

10

PowerShell을 v2의 새로운 Add-Type 기능을 사용하면 사용자 정의 클래스를 정교하게 할 수 있습니다 전형적인 WebRequest를 만드십시오. 인증에 사용할 수있는 인증서를 추가 할 수 있도록 사용자 지정 클래스에 메서드를 포함했습니다.

PS C:\> $def = @" 
public class ClientCertWebClient : System.Net.WebClient 
{ 
    System.Net.HttpWebRequest request = null; 
    System.Security.Cryptography.X509Certificates.X509CertificateCollection certificates = null; 

    protected override System.Net.WebRequest GetWebRequest(System.Uri address) 
    { 
     request = (System.Net.HttpWebRequest)base.GetWebRequest(address); 
     if (certificates != null) 
     { 
      request.ClientCertificates.AddRange(certificates); 
     } 
     return request; 
    } 

    public void AddCerts(System.Security.Cryptography.X509Certificates.X509Certificate[] certs) 
    { 
     if (certificates == null) 
     { 
      certificates = new System.Security.Cryptography.X509Certificates.X509CertificateCollection(); 
     } 
     if (request != null) 
     { 
      request.ClientCertificates.AddRange(certs); 
     } 
     certificates.AddRange(certs); 
    } 
} 
"@ 

PS C:\> Add-Type -TypeDefinition $def 

당신은 아마 인증서가 여기에 단지 현재 사용자 저장소에 사용 가능한 모든 인증서를 사용하는 대신 사용하려는 것이나, 단지 하나 (또는 ​​사람)에 추가되는 제한 할 것이라고는 예입니다 단지 모두로드 :

PS C:\> $wc = New-Object ClientCertWebClient 
PS C:\> $certs = dir cert:\CurrentUser\My 
PS C:\> $wc.AddCerts($certs) 
PS C:\> $wc.DownloadString("http://stackoverflow.com") 
+0

이것은 완벽합니다! 고마워요! – icnivad

관련 문제