public bool updateMembership(string RoleOrGroup, string action, string memberId, string parentId, ref string strErrors) { string method; if (action.ToLower() == "add" || action.ToLower() == "post") method = "POST"; else if (action.ToLower() == "delete" || action.ToLower() == "remove") method = "DELETE"; else return false; urlLink link = new urlLink(); link.url = this.BaseGraphUri + "/directoryObjects/" + memberId; string graphUri = ""; if (RoleOrGroup.ToUpper() == "GROUP") graphUri = this.BaseGraphUri + "/groups/" + parentId + "/$links" + "/members" + "?" + ApiVersion; else graphUri = this.BaseGraphUri + "/roles/" + parentId + "/$links" + "/members" + "?" + ApiVersion; if (method == "DELETE") { link.url = ""; if (RoleOrGroup.ToUpper() == "GROUP") graphUri = this.BaseGraphUri + "/groups/" + parentId + "/$links" + "/members/" + memberId + "?" + ApiVersion; else graphUri = this.BaseGraphUri + "/roles/" + parentId + "/$links" + "/members/" + memberId + "?" + ApiVersion; } if (this.updateLink(graphUri, method, link, ref strErrors)) return true; else return false; }
/// <summary> /// Set user manager. /// </summary> /// <param name="userPrincipalName">User UPN.</param> /// <param name="managerUserPrincipalName">Manager UPN.</param> /// <param name="strErrors">Error return value.</param> /// <returns>set object</returns> public bool SetUserManager(string userPrincipalName, string managerUserPrincipalName, ref string strErrors) { // construct URI, method, and managerLink string updateManagerURI = this.graphCall.BaseGraphUri + "/users/" + userPrincipalName + "/$links/manager?" + this.graphCall.ApiVersion; urlLink managerlink = new urlLink(); string method; if (managerUserPrincipalName != "NO MANAGER") { AadUser manager = this.graphCall.getUser(managerUserPrincipalName, ref strErrors); if (manager == null) { return false; } managerlink.url = this.graphCall.BaseGraphUri + "/directoryObjects/" + manager.objectId; method = "PUT"; } else { managerlink.url = null; method = "DELETE"; } return this.graphCall.updateLink(updateManagerURI, method, managerlink, ref strErrors); }
public bool updateLink(string uri, string method, urlLink link, 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; //Setup linked object JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); jsonSettings.NullValueHandling = NullValueHandling.Ignore; string body = JsonConvert.SerializeObject(link, 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" || method == "DELETE") { 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 == "POST") && ((response.StatusCode != HttpStatusCode.Created) || // (response.StatusCode != HttpStatusCode.OK) || (response.StatusCode != HttpStatusCode.NoContent))) if (updateHasResponseError(response)) { 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; } }