public bool modifyApplication(string method, AadApplication application, ref string strErrors) { // check if token is expired or about to expire in 2 minutes if (this.aadAuthentication.AadAuthenticationResult.IsExpired() || this.aadAuthentication.AadAuthenticationResult.WillExpireIn(2)) this.aadAuthentication.AadAuthenticationResult = this.aadAuthentication.GetNewAuthenticationResult(ref strErrors); if (this.aadAuthentication.AadAuthenticationResult == null) return false; string authnHeader = "Authorization: " + this.aadAuthentication.AadAuthenticationResult.AccessToken; string uri = this.BaseGraphUri + "/applications/" + application.objectId + "?" + this.ApiVersion; //Setup serialization JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); jsonSettings.NullValueHandling = NullValueHandling.Ignore; string body = ""; if (uri.Contains("/applications")) body = JsonConvert.SerializeObject(application, jsonSettings); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); request.Headers.Add(authnHeader); request.Method = method; if (method == "POST" || method == "PATCH" || method == "PUT") { byte[] data = encoding.GetBytes(body); request.Method = method; request.ContentType = "application/json"; request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if ((method == "GET" && response.StatusCode != HttpStatusCode.OK) || (method == "POST" && response.StatusCode != HttpStatusCode.Created) || (method == "PATCH" && response.StatusCode != HttpStatusCode.NoContent) || (method == "DELETE" && response.StatusCode != HttpStatusCode.NoContent)) { throw new Exception(String.Format( "Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription)); } else using (var stream = response.GetResponseStream()) { return true; } } } catch (WebException webException) { GraphHelperEventSourceLogger.Log(webException, ref strErrors); return false; } }
public AadApplication createApplication(AadApplication application, ref string strErrors) { // check if token is expired or about to expire in 2 minutes if (this.aadAuthentication.AadAuthenticationResult.IsExpired() || this.aadAuthentication.AadAuthenticationResult.WillExpireIn(2)) this.aadAuthentication.AadAuthenticationResult = this.aadAuthentication.GetNewAuthenticationResult(ref strErrors); if (this.aadAuthentication.AadAuthenticationResult == null) return null; string authnHeader = "Authorization: " + this.aadAuthentication.AadAuthenticationResult.AccessToken; string uri = this.BaseGraphUri + "/applications" + "?" + this.ApiVersion; //Setup object JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); jsonSettings.NullValueHandling = NullValueHandling.Ignore; string body = ""; body = JsonConvert.SerializeObject(application, jsonSettings); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); request.Headers.Add(authnHeader); request.Method = "POST"; request.ContentType = "application/json"; byte[] data = encoding.GetBytes(body); request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode != HttpStatusCode.Created) { throw new Exception(String.Format( "Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription)); } else using (var stream = response.GetResponseStream()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AadApplication)); AadApplication newApplication = ser.ReadObject(stream) as AadApplication; return newApplication; } } } catch (WebException webException) { GraphHelperEventSourceLogger.Log(webException, ref strErrors); return null; } }