private AADApplication GetAzureADApplication(string accessToken, string displayName) { AADApplication app = null; string url = string.Format(GraphUrl + "&$filter=displayName eq '{1}'" , "myorganization/applications", displayName); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Headers.Add(HttpRequestHeader.Authorization, string.Format("{0} {1}", "Bearer", accessToken)); request.UserAgent = "http://www.vipswapper.com/cloudstack"; WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(stream)) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AADApplicationResult)); var appResult = (AADApplicationResult)ser.ReadObject(stream); if (appResult.Applications.Length > 0) { app = appResult.Applications[0]; } } } return(app); }
private void CreateAzureADApplication(string accessToken, AADApplication app) { string url = string.Format(GraphUrl, "myorganization/applications/"); string postData; DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AADApplication)); using (MemoryStream stream = new MemoryStream()) { ser.WriteObject(stream, app); postData = Encoding.Default.GetString(stream.ToArray()); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); byte[] data = encoding.GetBytes(postData); request.Method = "POST"; request.Headers.Add(HttpRequestHeader.Authorization, string.Format("{0} {1}", "Bearer", accessToken)); request.ContentType = "application/json"; 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(); }
public void RegisterAzureADApplication(string accessToken, string Url) { AADApplication app = new AADApplication(); app.DisplayName = "CloudStack-" + Environment.MachineName; app.Homepage = Url; app.IdentifierUris = new string[1] { Url }; app.ReplyUrls = new string[1] { Url }; app.RequriredResourceAccess = new AADRequriredResourceAccess[2] { new AADRequriredResourceAccess { //CloudStack needs delegated access to Azure Active Directory Graph API ResourceAppId = "00000002-0000-0000-c000-000000000000", ResourceAccess = new AADResourceAccess [2] { //Sign-in and read user profile OAuth2Permission new AADResourceAccess { Id = "311a71cc-e848-46a1-bdf8-97ff7156d8e6", Type = "Scope" }, //Read all users' basic profiles OAuth2Permission new AADResourceAccess { Id = "cba73afc-7f69-4d86-8450-4978e04ecd1a", Type = "Scope" } } }, new AADRequriredResourceAccess { //CloudStack needs delegated access to Azure Resource Manager API ResourceAppId = "797f4846-ba00-4fd7-ba43-dac1f8f63013", ResourceAccess = new AADResourceAccess [1] { //Access Azure Service Management OAuth2Permission new AADResourceAccess { Id = "41094075-9dad-400e-a0bd-54e686782033", Type = "Scope" } } } }; app.KeyCredentials = new AADKeyCredential[1] { CreateAzureADKeyCredential(KeyCredentialPath) }; var existingApp = GetAzureADApplication(accessToken, app.DisplayName); if (existingApp != null) RemoveAzureADApplication(accessToken, existingApp.ObjectId); CreateAzureADApplication(accessToken, app); }