コード例 #1
0
        private void OnAcceptCommand()
        {
            if (_sVerificationCode == "")
            {
                return;
            }

            Log.DebugFormat("Sending verification code {0}", _sVerificationCode);

            // Create a collection object and populate it using the PFX file

            bool   bResult        = true;
            string sStationNumber = null;

#if STAGINGUITEST
            sStationNumber = _sVerificationCode.Substring(0, 4);
#else
            if (System.Diagnostics.Debugger.IsAttached && string.IsNullOrEmpty(ConfigurationManager.AppSettings["CertificateUrl"]))
            {
                sStationNumber = _sVerificationCode.Substring(0, 4);
            }
            else if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["CertificateUrl"]))
            {
                string url = ConfigurationManager.AppSettings["CertificateUrl"];

                HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(url);
                ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); };

                ASCIIEncoding encoding = new ASCIIEncoding();
                string        postData = "c=" + _sVerificationCode;
                byte[]        data     = encoding.GetBytes(postData);

                httpWReq.Method        = "POST";
                httpWReq.ContentType   = "application/x-www-form-urlencoded";
                httpWReq.ContentLength = data.Length;

                using (Stream stream = httpWReq.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                HttpWebResponse  response   = (HttpWebResponse)httpWReq.GetResponse();
                System.IO.Stream dataStream = response.GetResponseStream();

                System.IO.BinaryReader streamReader = new System.IO.BinaryReader(dataStream);

                IList <byte[]> array = new List <byte[]>();
                do
                {
                    byte[] tmpfile = streamReader.ReadBytes(1024);
                    array.Add(tmpfile);
                } while (array.Last().Length == 1024);

                byte[] file = new byte[array.Sum(x => x.Length)];

                int i = 0;
                foreach (var bytese in array)
                {
                    foreach (var b in bytese)
                    {
                        file[i++] = b;
                    }
                }


                dataStream.Close();
                streamReader.Close();
                string certFilename = DateTime.Now.ToFileTime() + "cert.p12";

                FileStream   fs = new FileStream(certFilename, FileMode.CreateNew);
                BinaryWriter bw = new BinaryWriter(fs);

                for (int j = 0; j < file.Length; j++)
                {
                    bw.Write(file[j]);
                }

                streamReader.Close();
                response.Close();
                fs.Flush();
                fs.Close();

                try
                {
                    X509Certificate2 cert = new X509Certificate2(file, _sVerificationCode);
                    var serviceRuntimeUserCertificateStore = new X509Store(StoreName.My);
                    serviceRuntimeUserCertificateStore.Open(OpenFlags.ReadWrite);
                    foreach (var certificate in serviceRuntimeUserCertificateStore.Certificates)
                    {
                        if (certificate.Subject.Contains("Sportradar AG"))
                        {
                            serviceRuntimeUserCertificateStore.Remove(certificate);
                            break;
                        }
                    }

                    var process   = new Process();
                    var startInfo = new ProcessStartInfo();
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    startInfo.FileName    = "certutil.exe";
                    startInfo.Arguments   = " -f -user -p " + _sVerificationCode + " -importpfx " + certFilename;
                    process.StartInfo     = startInfo;
                    process.Start();

                    serviceRuntimeUserCertificateStore.Close();
                    sStationNumber = cert.Subject.Substring(cert.Subject.IndexOf("CN=") + 3, cert.Subject.IndexOf(", ", cert.Subject.IndexOf("CN=")) - cert.Subject.IndexOf("CN=") - 3);
                    sStationNumber = sStationNumber.Replace("terminal", "");
                }
                catch (Exception e)
                {
                    Log.Error(e.Message, e);
                    ShowError(TranslationProvider.Translate(MultistringTags.CERTIFICATE_ERROR).ToString());
                    return;
                }
            }
            else
            {
                try
                {
                    sStationNumber = WsdlRepository.GetStationByVerificationNumber(_sVerificationCode);
                }
                catch (Exception ex)
                {
                    Log.Error("", ex);
                    ShowError(ex.Message);
                    return;
                }
            }
#endif
            try
            {
                if (bResult)
                {
                    Log.InfoFormat("Saving station number {0} to database", sStationNumber);
                    StationRepository.SetStationAppConfigValue("StationNumber", sStationNumber);
                }
            }
            catch (Exception Ex)
            {
                Log.Error("", Ex);
                ShowError("Can't connect to database " + Ex.Message);
                return;
            }

            _sVerificationCode = "";
            Log.Debug("Closing verification window");

            /* ChangeTracker.VerifivationCancelled = false;
             * ChangeTracker.VerificationRestart = true;*/
            Log.Debug("Exiting verification window");
            Mediator.SendMessage <long>(0, MsgTag.RestartApplication);
        }