public bool licenseUser(AadUser user, string skuToAddOrUpdate, string skuToRemove, string[] disabledPlans, ref string strErrors) { // if incorrect parameters are passed in, return null if (user == null || (skuToAddOrUpdate == skuToRemove)) return false; // create the addLicense object addLicense licenseObjectToAdd = new addLicense(); // assign the subscriptionId to the addLicense object licenseObjectToAdd.skuId = skuToAddOrUpdate; //get the list of plans to remove and assign to the addLicense Object licenseObjectToAdd.disabledPlans = disabledPlans; //create a List of addLicense objects, and add the addLicense object List<addLicense> addLicensesList = new List<addLicense>(); // if skuToAddOrUpdate is not empty, then add the license object, else remove the license Object if (skuToAddOrUpdate != "") addLicensesList.Add(licenseObjectToAdd); else addLicensesList.Clear(); // assign addLicense and removeLicense objects to license object userLicense license = new userLicense(); license.addLicenses = addLicensesList; // check to see if there's a skue to remove if (skuToRemove != "") { string[] licenseToRemove = new string[1] { skuToRemove }; license.removeLicenses = licenseToRemove; } else license.removeLicenses = new string[0] { }; AadUser returnedUser = this.licenseUser(user, license, ref strErrors); if (returnedUser != null) return true; else return false; }
public AadUser licenseUser(AadUser user, userLicense license, ref string strErrors) { // return null, if the user's location location is not populated, then we can't assign a license if (user.usageLocation == null) return null; // 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; //Setup AadUser object JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); jsonSettings.NullValueHandling = NullValueHandling.Ignore; string body = JsonConvert.SerializeObject(license, jsonSettings); //Console.WriteLine(body); string method = "POST"; string authnHeader = "Authorization: " + this.aadAuthentication.AadAuthenticationResult.AccessToken; string uri = this.BaseGraphUri + "/users/" + user.userPrincipalName + "/assignLicense" + "?" + StringConstants.ApiVersionPreview; 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 == "POST" && response.StatusCode != HttpStatusCode.Created) && response.StatusCode != HttpStatusCode.OK ) { throw new Exception(String.Format( "Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription)); } else if ((method == "PATCH") || (method == "PUT") || (method == "POST") ) using (var stream = response.GetResponseStream()) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AadUser)); AadUser newUser = ser.ReadObject(stream) as AadUser; return newUser; } else return null; } } catch (WebException webException) { GraphHelperEventSourceLogger.Log(webException, ref strErrors); return null; } }