authentication - How can you add a Certificate to WebClient in Powershell -
i wan't examine webpage requires client side certificate authentication. how can provide cert certstore webrequest: there way specify in credentials odr within proxy?
$webclient = new-object net.webclient # next 5 lines required if network has proxy server $webclient.credentials = [system.net.credentialcache]::defaultcredentials if($webclient.proxy -ne $null) { $webclient.proxy.credentials = ` [system.net.credentialcache]::defaultnetworkcredentials } # main call $output = $webclient.downloadstring("$url") ps: maybe helps: how can add certificate webclient (c#)? don't it.. ;-)
using new add-type functionality in powershell v2, can craft custom class can use make typical webrequest. have included method on custom class allow add certificates can used authentication.
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 you perhaps want limit certificates being added 1 (or ones) want use rather use every available certificate in current user store, here example loads of them:
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")
Comments
Post a Comment