コード例 #1
0
        public DeviceAuthCodeResponse GetDeviceAuthCode(string aadId)
        {
            DeviceAuthCodeResponse codeResponse = null;
            string url = string.Format(DeviceCodeUrl, aadId);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            string postData = "client_id=" + HttpUtility.UrlEncode(ClientID);

            postData += "&resource=" + HttpUtility.UrlEncode(GraphResourceUri);
            byte[] data = encoding.GetBytes(postData);
            request.Method        = "POST";
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.UserAgent     = "http://www.vipswapper.com/cloudstack";
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            WebResponse response = request.GetResponse();

            using (Stream stream = response.GetResponseStream())
            {
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DeviceAuthCodeResponse));
                codeResponse = (DeviceAuthCodeResponse)ser.ReadObject(stream);
            }

            return(codeResponse);
        }
コード例 #2
0
        public DeviceAuthTokenResponse GetDeviceAuthToken(DeviceAuthCodeResponse codeResponse, string aadId)
        {
            DeviceAuthTokenResponse tokenResponse = null;
            string url = string.Format(TokenUrl, aadId);

            while (true)
            {
                HttpWebRequest            request  = (HttpWebRequest)WebRequest.Create(url);
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                string postData = "grant_type=device_code";
                postData += "&client_id=" + HttpUtility.UrlEncode(ClientID);
                postData += "&resource=" + HttpUtility.UrlEncode(GraphResourceUri);
                postData += "&code=" + HttpUtility.UrlEncode(codeResponse.DeviceCode);
                byte[] data = encoding.GetBytes(postData);
                request.Method        = "POST";
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.UserAgent     = "http://www.vipswapper.com/cloudstack";
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                WebResponse response = null;
                try
                {
                    response = request.GetResponse();
                    using (Stream stream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DeviceAuthTokenResponse));
                        tokenResponse = (DeviceAuthTokenResponse)ser.ReadObject(stream);
                    }
                    break;
                }
                catch (WebException ex)
                {
                    if (ex.Response != null)
                    {
                        using (Stream stream = ((HttpWebResponse)ex.Response).GetResponseStream())
                        {
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DeviceAuthTokenResponse));
                            tokenResponse = (DeviceAuthTokenResponse)ser.ReadObject(stream);
                        }
                        if (tokenResponse.Error.Equals("authorization_pending", StringComparison.InvariantCultureIgnoreCase))
                        {
                            Thread.Sleep(codeResponse.Interval * 1000);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return(tokenResponse);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            CloudStackConsoleInstaller installer = new CloudStackConsoleInstaller();

            Console.Write("---------- CloudStack Installer ----------\n");
            Console.Write("Hi there! \nFirst, this installer will download the latest CloudStack binaries." +
                          " \nThen, it will configure a new website for CloudStack on this IIS server. \nFinally, it will register the" +
                          " instance of CloudStack with your Azure Active Directory so that it can manage your Azure cloud.\n\n");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("Sounds good? If so, hit enter. If not, close this widow.");
            Console.ResetColor();
            Console.ReadLine();
            Console.Write("\n1) Downloading CloudStack binaries ... ");
            installer.DummyMethodDownloadCloudStackBinaries();
            Console.Write("Done. \n\n2) Installing CloudStack website on this web server ... ");
            installer.DummyMethodInstallCloudStackWebsite();
            Console.Write("Done. \n\n3) Alright, let's connect this CloudStack instance with your Azure Cloud - ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("please enter the id of your Azure subscription that you wish to connect: ");
            Console.ResetColor();
            string subscriptionId = Console.ReadLine();
            string aadId          = installer.GetAADForSubscription(subscriptionId);
            DeviceAuthCodeResponse codeResponse = installer.GetDeviceAuthCode(aadId);

            Console.Write("Got it. \nNow, you must authenticate with the account that you use to manage your Azure cloud - ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("use a web browser to open the page {0} and enter the code {1}.", codeResponse.VerificationUrl, codeResponse.UserCode);
            Console.ResetColor();
            Console.Write("\nPatiently waiting ... ");
            DeviceAuthTokenResponse tokenResponse = installer.GetDeviceAuthToken(codeResponse, aadId);
            AADUser user = installer.GetAzureADUser(tokenResponse.AccessToken);

            Console.Write("Ah! welcome {0}! Registering CloudStack in your Azure Active Directory now ... ", user.DisplayName);
            installer.RegisterAzureADApplication(tokenResponse.AccessToken, "http://localhost");
            Console.Write(" All done.");
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: bstearns/VipSwapper
        public DeviceAuthTokenResponse GetDeviceAuthToken(DeviceAuthCodeResponse codeResponse, string aadId)
        {
            DeviceAuthTokenResponse tokenResponse = null;
            string url = string.Format(TokenUrl, aadId);

            while (true)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                string postData = "grant_type=device_code";
                postData += "&client_id=" + HttpUtility.UrlEncode(ClientID);
                postData += "&resource=" + HttpUtility.UrlEncode(GraphResourceUri);
                postData += "&code=" + HttpUtility.UrlEncode(codeResponse.DeviceCode);
                byte[] data = encoding.GetBytes(postData);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = data.Length;
                request.UserAgent = "http://www.vipswapper.com/cloudstack";
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                WebResponse response = null;
                try
                {
                    response = request.GetResponse();
                    using (Stream stream = response.GetResponseStream())
                    {
                        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DeviceAuthTokenResponse));
                        tokenResponse = (DeviceAuthTokenResponse)ser.ReadObject(stream);
                    }
                    break;
                }
                catch (WebException ex)
                {
                    if (ex.Response != null)
                    {
                        using (Stream stream = ((HttpWebResponse)ex.Response).GetResponseStream())
                        {
                            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DeviceAuthTokenResponse));
                            tokenResponse = (DeviceAuthTokenResponse)ser.ReadObject(stream);
                        }
                        if (tokenResponse.Error.Equals("authorization_pending", StringComparison.InvariantCultureIgnoreCase))
                        {
                            Thread.Sleep(codeResponse.Interval * 1000);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            return tokenResponse;
        }