internal virtual void PrepareSession() { // make sure we have a valid WebRequestSession object to work with if (null == WebSession) { WebSession = new WebRequestSession(); } if (null != SessionVariable) { // save the session back to the PS environment if requested PSVariableIntrinsics vi = SessionState.PSVariable; vi.Set(SessionVariable, WebSession); } // // handle credentials // if (null != Credential && Authentication == WebAuthenticationType.None) { // get the relevant NetworkCredential NetworkCredential netCred = Credential.GetNetworkCredential(); WebSession.Credentials = netCred; // supplying a credential overrides the UseDefaultCredentials setting WebSession.UseDefaultCredentials = false; } else if ((null != Credential || null != Token) && Authentication != WebAuthenticationType.None) { ProcessAuthentication(); } else if (UseDefaultCredentials) { WebSession.UseDefaultCredentials = true; } if (null != CertificateThumbprint) { X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates; X509Certificate2Collection tbCollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByThumbprint, CertificateThumbprint, false); if (tbCollection.Count == 0) { CryptographicException ex = new CryptographicException(WebCmdletStrings.ThumbprintNotFound); throw ex; } foreach (X509Certificate2 tbCert in tbCollection) { X509Certificate certificate = (X509Certificate)tbCert; WebSession.AddCertificate(certificate); } } if (null != Certificate) { WebSession.AddCertificate(Certificate); } // // handle the user agent // if (null != UserAgent) { // store the UserAgent string WebSession.UserAgent = UserAgent; } if (null != Proxy) { WebProxy webProxy = new WebProxy(Proxy); webProxy.BypassProxyOnLocal = false; if (null != ProxyCredential) { webProxy.Credentials = ProxyCredential.GetNetworkCredential(); } else if (ProxyUseDefaultCredentials) { // If both ProxyCredential and ProxyUseDefaultCredentials are passed, // UseDefaultCredentials will overwrite the supplied credentials. webProxy.UseDefaultCredentials = true; } WebSession.Proxy = webProxy; } if (-1 < MaximumRedirection) { WebSession.MaximumRedirection = MaximumRedirection; } // store the other supplied headers if (null != Headers) { foreach (string key in Headers.Keys) { // add the header value (or overwrite it if already present) WebSession.Headers[key] = Headers[key].ToString(); } } }