Esempio n. 1
0
        public async Task <IActionResult> CreateProfileAsync([FromForm] CreateRestProfileModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Index", model));
            }

            var profile = await GetProfileAsync(model.DisplayName);

            if (profile != null)
            {
                return(RedirectToAction("ProfilesAsync", new { profileId = profile["id"] }));
            }

            var profileToCreate = await GenerateProfileAsync(model);

            var createProfileRequest = await manager.PostProfileAsync(profileToCreate);

            var createProfileContent = await createProfileRequest.Content.ReadAsStringAsync();

            var createdProfile = JObject.Parse(createProfileContent);

            if (createdProfile.ContainsKey("error"))
            {
                var formattedError = createdProfile.ToString(Formatting.Indented);
                ViewBag.creationError = formattedError;

                _logger.LogError("Error while creating profile:\r\n" + formattedError);
                return(View("Index", model));
            }

            _logger.LogInformation("Created profile:\r\n" + createdProfile.ToString(Formatting.Indented));

            return(RedirectToAction("ProfilesAsync", new { profileId = createdProfile["id"] }));
        }
Esempio n. 2
0
        private async Task <JObject> GetProfileAsync(string profileType)
        {
            // find existing matching profile
            var profileName         = $"OneRoster{profileType.ToUpper()}Profile";
            HttpResponseMessage res = await manager.QueryAllProfilesAsync();

            var profiles = (JArray)JObject.Parse(await res.Content.ReadAsStringAsync())["value"];

            foreach (var profile in profiles)
            {
                if ((string)profile["displayName"] == profileName)
                {
                    return((JObject)profile);
                }
            }

            // create new profile
            var generatedProfile = await GenerateProfileAsync(profileType);

            HttpResponseMessage res2 = await manager.PostProfileAsync(generatedProfile);

            return(JObject.Parse(await res2.Content.ReadAsStringAsync()));
        }