View Single Post
  #1 (permalink)  
Old June 28th, 2007, 08:05 AM
kalchev kalchev is offline
Authorized User
 
Join Date: Mar 2006
Location: Sofia, , Bulgaria.
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kalchev
Default certificate pfx c# gives me an err ".forbidden 403

Hello averyone,
I have a certificate file "cert.pfx" and I've tried to use it all those ways showed bellow in the code. All the time I recieve the err:

"{System.Net.WebException: The remote server returned an error: (403) Forbidden.
   at System.Net.HttpWebRequest.GetResponse()...}"
I've imported the certificate in all possible the Certificates in the mmc Local Computer and User and still the same. The certificate password is perfect working when I taste it with the software given me from the company I bought the service. So any help is really welcom. Here is the source (One of the idea is that migh have some diificultis in the part of handshaking or whatever they called it):

Code:
            try
            {
                HttpWebRequestC2D = (HttpWebRequest)HttpWebRequest.Create(sConnectStringUrl);
                HttpWebRequestC2D.Method = "POST";
                HttpWebRequestC2D.ContentType = "text/xml";
                //X509Certificate x509 = X509Certificate.CreateFromCertFile(sCertFileName);
                //X509Certificate x509_1 = new System.Security.Cryptography.X509Certificates.X509Certificate2(sCertFileName, @"*******");
                //X509Certificate x509_1 = new System.Security.Cryptography.X509Certificates.X509Certificate2(File.ReadAllBytes(sCertFileName), @"*********", X509KeyStorageFlags.MachineKeySet);
                X509Certificate x509_1 = new System.Security.Cryptography.X509Certificates.X509Certificate2(File.ReadAllBytes(sCertFileName), @"*******", X509KeyStorageFlags.MachineKeySet);

                X509Store storeMyUser = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                X509Store storeMachine = new X509Store(StoreLocation.LocalMachine);

                storeMyUser.Open(OpenFlags.MaxAllowed);
                storeMachine.Open(OpenFlags.MaxAllowed);

                //HttpWebRequestC2D.Headers.Add();
                X509Certificate2Collection x509CertCollectUser = storeMyUser.Certificates.Find(X509FindType.FindBySerialNumber, @"17 84 75 4f f3 74 a4 4e ae 40 d5 1b d2 bd c2 5e", false);
                X509Certificate2Collection x509CertCollectMachine = storeMachine.Certificates.Find(X509FindType.FindBySerialNumber, @"17 84 75 4f f3 74 a4 4e ae 40 d5 1b d2 bd c2 5e", false);

                HttpWebRequestC2D.ClientCertificates.Add(x509_1);
                for (int i = 0; i < x509CertCollectUser.Count;i++)
                    HttpWebRequestC2D.ClientCertificates.Add(x509CertCollectUser[i]);
                for (int i = 0; i < x509CertCollectMachine.Count; i++)
                    HttpWebRequestC2D.ClientCertificates.Add(x509CertCollectMachine[i]);

                storeMyUser.Close();
                storeMachine.Close();
                //System.Security.Cryptography.X509Certificates.
                //PFXExportCertStore();

                StreamWriter StreamWriterC2D = new StreamWriter(HttpWebRequestC2D.GetRequestStream());

                XmlTextWriter XmlTextWriterRequest = new XmlTextWriter(StreamWriterC2D);

                XmlTextWriterRequest.WriteProcessingInstruction("xml", @"version=""1.0"" encoding=""utf-8""");
                //XmlTextWriterRequest.WriteDocType("REQUEST_GROUP", null, "C2DRequestv2.0.dtd", null);


                XmlTextWriterRequest.WriteStartElement("OFX");

                XmlTextWriterRequest.WriteStartElement("SIGNONMSGSRQV1");
                XmlTextWriterRequest.WriteStartElement("SONRQ");
                XmlTextWriterRequest.WriteElementString("DTCLIENT", "20020725092657");
                XmlTextWriterRequest.WriteElementString("USERID", sUser);
                XmlTextWriterRequest.WriteElementString("USERPASS", sPassword);
                XmlTextWriterRequest.WriteElementString("LANGUAGE", "ENG");
                XmlTextWriterRequest.WriteElementString("APPID", "11111111");
                XmlTextWriterRequest.WriteElementString("APPVER", "2");
                XmlTextWriterRequest.WriteEndElement();
                XmlTextWriterRequest.WriteEndElement();

                XmlTextWriterRequest.WriteStartElement("EIVVERMSGSRQV1");
                XmlTextWriterRequest.WriteStartElement("EIVEMPLOYMENTTRNRQ");
                XmlTextWriterRequest.WriteElementString("TRNUID", "1234567890");
                XmlTextWriterRequest.WriteStartElement("TRNPURPOSE");
                XmlTextWriterRequest.WriteElementString("CODE", "PPCREDIT");
                XmlTextWriterRequest.WriteEndElement(); //TRNPURPOSE
                XmlTextWriterRequest.WriteStartElement("EIVEMPLOYMENTRQ");
                XmlTextWriterRequest.WriteElementString("EMPLOYERCODE", "99999");
                XmlTextWriterRequest.WriteElementString("EIVEMPLOYEEID", "001012414");
                XmlTextWriterRequest.WriteEndElement(); //EIVEMPLOYMENTRQ
                XmlTextWriterRequest.WriteEndElement();////EIVEMPLOYMENTTRNRQ
                XmlTextWriterRequest.WriteEndElement();////EIVVERMSGSRQV1

                XmlTextWriterRequest.WriteEndElement();//root OFX

                XmlTextWriterRequest.Close();

                HttpWebResponseC2D = (HttpWebResponse)HttpWebRequestC2D.GetResponse();


Thank You,
G