Click here to Skip to main content
15,906,219 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! Have some troubles - when i try to download xml file with using SSL and certificate authentication(with proxy), it does not working(( It work when i try download file from another local machine, when i try download from here for example: http://bank-ua.com/export/currrate.xml What is my problem?
C#
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;
using SummaTechnology.CSTI.Common.Serializer;
using SummaTechnology.CSTI.Common.Utils;
using SummaTechnology.CSTI.Common.DataAccess;

namespace SelectClientCert
{
    class MyCerts
    {   
        private static int CERT_STORE_PROV_SYSTEM = 10;
        private static int CERT_SYSTEM_STORE_CURRENT_USER = (1 << 16);
        //private static int CERT_SYSTEM_STORE_LOCAL_MACHINE = (2 << 16);

        [DllImport("CRYPT32", EntryPoint = "CertOpenStore", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern IntPtr CertOpenStore(
            int storeProvider, int encodingType,
            int hcryptProv, int flags, string pvPara);

        [DllImport("CRYPT32", EntryPoint = "CertEnumCertificatesInStore", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern IntPtr CertEnumCertificatesInStore(
            IntPtr storeProvider,
            IntPtr prevCertContext);

        [DllImport("CRYPT32", EntryPoint = "CertCloseStore", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern bool CertCloseStore(
            IntPtr storeProvider,
            int flags);

        X509Certificate2Collection m_certs;

        public MyCerts()
        {
            m_certs = new X509Certificate2Collection();
        }

        public int Init()
        {
            IntPtr storeHandle;
            storeHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER, "MY");
            IntPtr currentCertContext;
            currentCertContext = CertEnumCertificatesInStore(storeHandle, (IntPtr)0);
            int i = 0;
            while (currentCertContext != (IntPtr)0)
            {
                m_certs.Insert(i++, new X509Certificate2(currentCertContext));
                currentCertContext = CertEnumCertificatesInStore(storeHandle, currentCertContext);
            }
            CertCloseStore(storeHandle, 0);

            return m_certs.Count;
        }

        public X509Certificate2 this[int index]
        {
            get
            {
                if (index < 0 || index > m_certs.Count)
                    return null;
                else
                    return m_certs[index];
            }
        }
    }    
    class MyHttpResource
    {
        String m_url;

        public MyHttpResource(string url)
        {
            m_url = url;
        }

        public void GetFile()
        {
            string pro = "AF35AC9E7F6101DF461CC6F553088821856C7ECF";
            Console.WriteLine("GetFile start");
            HttpWebResponse result = null;            
            System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();    
            try
            {
                WebProxy proxy = new WebProxy("squid.tgk-4.ru:3128", true);
                proxy.Credentials = new NetworkCredential("CSTI-CS", "sde486t");                
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_url);
                
                req.Proxy = proxy;
                //req.Credentials = CredentialCache.DefaultCredentials;                
                MyCerts mycert = new MyCerts();                   
                int cerNum = mycert.Init();
                Console.WriteLine("\r\n\r\nUsing certificates");
                for (int i = 0; i < cerNum; i++)
                {                 
                    if (mycert[i].Thumbprint.Equals(pro)) { Console.WriteLine("pro"); req.ClientCertificates.Add(mycert[i]); }                                                              
                    Console.WriteLine("Certificate" + i.ToString() + " Thumbprint " + mycert[i].Thumbprint);
                }
                result = (HttpWebResponse)req.GetResponse();

                StreamWriter writer = new StreamWriter(@"C:/123.txt");
                Console.WriteLine("\r\n\r\nStart GetResponseStream in GetFile");
                Stream ReceiveStream = result.GetResponseStream();

                #region For XML files             
                //Encoding encode = System.Text.Encoding.Default;
                #endregion

                #region For sites                
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
                #endregion
                Console.WriteLine("GetResponseStream complete\r\n\r\n");
                Console.WriteLine("result.IsMutuallyAuthenticated:  " + result.IsMutuallyAuthenticated);                
                Console.WriteLine("result.ResponseUri:  " + result.ResponseUri);
                Console.WriteLine("result.Headers:  " + result.Headers);               

                StreamReader sr = new StreamReader(ReceiveStream, encode);
                Console.WriteLine("\r\nResponse stream received");
                Char[] read = new Char[256];
                int count = sr.Read(read, 0, 256);

                Console.WriteLine("HTTP Response...\r\n");
                Console.WriteLine("Write into file");
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    Console.Write(str);
                    writer.WriteLine(str);
                    count = sr.Read(read, 0, 256);
                }
                writer.Close();
            }
            catch (WebException e)
            {
                Console.WriteLine(e.Message); 				
            }
            finally
            {
                if (result != null)
                {
                    result.Close();
                }
            }
        }
    }
    
    public class MyPolicy : ICertificatePolicy
    {
        public bool CheckValidationResult(
            ServicePoint srvPoint
            , X509Certificate certificate
            , WebRequest request
            , int certificateProblem)
        {
            return true;
        }
    }
   
    class CertSample
    {
        static void Main(string[] args)
        {         
            string str = @"Some protected certificate XML url ";                  
            try
            {
                MyHttpResource hr = new MyHttpResource(str);
                hr.GetFile();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ReadKey();
            return;
        }
    }
}
Posted

1 solution

Itz due to the local machine privilege issue; make sure that you have the rights to do so.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900