public DeviceLoginCodeResponse GetDeviceLoginCode(string directoryId) { DeviceLoginCodeResponse codeResponse = null; string url = string.Format(DeviceLoginCodeUrl, directoryId); 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(DeviceLoginCodeResponse)); codeResponse = (DeviceLoginCodeResponse)ser.ReadObject(stream); } return(codeResponse); }
public DeviceLoginTokenResponse GetDeviceLoginToken(DeviceLoginCodeResponse codeResponse, string directoryId) { DeviceLoginTokenResponse tokenResponse = null; string url = string.Format(TokenUrl, directoryId); 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(DeviceLoginTokenResponse)); tokenResponse = (DeviceLoginTokenResponse)ser.ReadObject(stream); } break; } catch (WebException ex) { if (ex.Response != null) { using (Stream stream = ((HttpWebResponse)ex.Response).GetResponseStream()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(DeviceLoginTokenResponse)); tokenResponse = (DeviceLoginTokenResponse)ser.ReadObject(stream); } if (tokenResponse.Error.Equals("authorization_pending", StringComparison.InvariantCultureIgnoreCase)) { Thread.Sleep(codeResponse.Interval * 1000); } else { break; } } } } return(tokenResponse); }
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? Hit enter to proceed."); 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 your Azure subscription id: "); Console.ResetColor(); string subscriptionId = Console.ReadLine(); string aadId = installer.GetDirectoryForSubscription(subscriptionId); DeviceLoginCodeResponse codeResponse = installer.GetDeviceLoginCode(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 ... "); DeviceLoginTokenResponse tokenResponse = installer.GetDeviceLoginToken(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:55651/"); Console.Write(" All done."); }