Exemplo n.º 1
0
        public void Register(Uri apiUri, CertificateStore localCertStore)
        {
            _localCertStore = localCertStore;

            //check if email address domain exists
            {
                try
                {
                    DnsClient dns = new DnsClient();
                    dns.Proxy = _proxy;

                    dns.ResolveMX(_localCertStore.Certificate.IssuedTo.EmailAddress);
                }
                catch (NameErrorDnsClientException)
                {
                    throw new NameErrorDnsClientException("The domain of your email address '" + _localCertStore.Certificate.IssuedTo.EmailAddress.Host + "' does not exists. Please check if you have entered correct email address.");
                }
                catch
                {
                    try
                    {
                        DnsDatagram response = DnsClient.ResolveViaRootNameServers(_localCertStore.Certificate.IssuedTo.EmailAddress.Host, DnsResourceRecordType.MX, _proxy);
                        if (response.Header.RCODE == DnsResponseCode.NameError)
                        {
                            throw new NameErrorDnsClientException("The domain of your email address '" + _localCertStore.Certificate.IssuedTo.EmailAddress.Host + "' does not exists. Please check if you have entered correct email address.");
                        }
                    }
                    catch (NameErrorDnsClientException)
                    {
                        throw;
                    }
                    catch
                    { }
                }
            }

            //verify self signed cert
            _localCertStore.Certificate.Verify(new Certificate[] { _localCertStore.Certificate });

            using (WebClientEx client = new WebClientEx())
            {
                client.Proxy     = _proxy;
                client.UserAgent = GetUserAgent();

                using (Stream s = client.OpenWriteEx(apiUri.AbsoluteUri + "?cmd=reg"))
                {
                    _localCertStore.Certificate.WriteTo(s);
                }

                using (BinaryReader bR = new BinaryReader(client.GetResponseStream()))
                {
                    int errorCode = bR.ReadInt32();
                    if (errorCode != 0)
                    {
                        string message          = Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadInt32()));
                        string remoteStackTrace = Encoding.UTF8.GetString(bR.ReadBytes(bR.ReadInt32()));

                        throw new Exception(message);
                    }
                }
            }
        }