public AzureList()
        {
            InitializeComponent();

            var viewModel = new AzureListViewModel();

            this.BindingContext = viewModel;
        }
Ejemplo n.º 2
0
        public AzureListViewModel getAzureList(string subscriptionID)
        {
            SqlRestMetadata objMetdata = new SqlRestMetadata();

            try
            {
                //First read the certificate from certificate store which will be used to authenticate the request.
                X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
                certificateStore.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certificates = certificateStore.Certificates;
                certificateThumbprint = Regex.Replace(certificateThumbprint, @"[^\u0000-\u007F]", string.Empty); // to remove whitespace.
                var matchingCertificates = certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, false);
                var managementCert       = matchingCertificates[0];

                X509Certificate2 cert       = new X509Certificate2(certificateFile);
                string           requestUrl = string.Format(CultureInfo.InvariantCulture,
                                                            "https://management.database.windows.net:8443/{0}/servers", subscriptionID);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestUrl);
                req.Method = "GET";
                req.Headers.Add("x-ms-version", "1.0");
                req.ClientCertificates.Add(managementCert);

                using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                {
                    _list = new List <SqlRestServiceData>();
                    objMetdata.Requestid        = resp.Headers["x-ms-request-id"];
                    objMetdata.returnStatusCode = (int)((HttpWebResponse)resp).StatusCode;

                    using (Stream stream = resp.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(stream))
                        {
                            var        xdoc = XElement.Load(sr);
                            XNamespace ns   = "http://schemas.microsoft.com/sqlazure/2010/12/";
                            foreach (var urlElement in xdoc.Elements(ns + "Server"))
                            {
                                SqlRestServiceData returnData = new SqlRestServiceData();
                                returnData.ServerName         = urlElement.Element(ns + "Name").Value;
                                returnData.AdminLogin         = urlElement.Element(ns + "AdministratorLogin").Value;
                                returnData.DataCenterLocation = urlElement.Element(ns + "Location").Value;
                                _list.Add(returnData);
                            }
                        }
                    }
                }
            }
            catch (WebException webEx)
            {
                var resp = webEx.Response;
                objMetdata                  = new SqlRestMetadata();
                objMetdata.Requestid        = resp.Headers["x-ms-request-id"];
                objMetdata.returnStatusCode = (int)((HttpWebResponse)resp).StatusCode;
                objMetdata.ErrorMsg         = webEx.Message;
            }

            var returnValues = new AzureListViewModel();

            returnValues.resultMetadata = objMetdata;
            returnValues.serverList     = _list;
            return(returnValues);
        }