示例#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"] }));
        }
示例#2
0
        public IActionResult Index()
        {
            var model = new CreateRestProfileModel
            {
                ConnectionUrl = $"{(Request.IsHttps ? "https" : "http")}://{Request.Host}/ims/oneroster/v1p1",
                DisplayName   = "OneRoster API",
                ClientId      = "contoso",
                ClientSecret  = "contoso-secret",
                AzureDomain   = _config.GetValue <string>("AzureDomain")
            };

            return(View(model));
        }
示例#3
0
        // https://github.com/OfficeDev/O365-EDU-Tools/blob/master/SDSProfileManagementDocs/api/synchronizationProfile_create.md
        private async Task <string> GenerateProfileAsync(CreateRestProfileModel profile)
        {
            var skus = await GetOfficeSkusAsync();

            StringBuilder sb = new StringBuilder();

            using (JsonWriter writer = new JsonTextWriter(new StringWriter(sb)))
            {
                writer.WriteStartObject();

                writer.WritePropertyName("displayName");
                writer.WriteValue(profile.DisplayName);

                writer.WritePropertyName("dataProvider");
                writer.WriteStartObject();

                writer.WritePropertyName("@odata.type");
                writer.WriteValue("#microsoft.graph.educationoneRosterApiDataProvider");

                writer.WritePropertyName("connectionUrl");
                writer.WriteValue(profile.ConnectionUrl);

                writer.WritePropertyName("schoolsIds");
                writer.WriteStartArray();
                foreach (var schoolId in profile.SchoolIds.Split(','))
                {
                    writer.WriteValue(schoolId);
                }
                writer.WriteEndArray();

                writer.WritePropertyName("connectionSettings");
                writer.WriteStartObject();

                writer.WritePropertyName("@odata.type");
                writer.WriteValue("#microsoft.graph.educationSynchronizationOAuth1ConnectionSettings");

                writer.WritePropertyName("clientId");
                writer.WriteValue(profile.ClientId);

                writer.WritePropertyName("clientSecret");
                writer.WriteValue(profile.ClientSecret);

                writer.WriteEndObject();

                writer.WriteEndObject();

                writer.WritePropertyName("identitySynchronizationConfiguration");
                writer.WriteStartObject();

                writer.WritePropertyName("@odata.type");
                writer.WriteValue("#microsoft.graph.educationidentitycreationconfiguration");

                writer.WritePropertyName("userDomains");
                writer.WriteStartArray();
                writer.WriteStartObject();
                writer.WritePropertyName("appliesTo");
                writer.WriteValue("student");

                writer.WritePropertyName("name");
                writer.WriteValue(profile.AzureDomain);
                writer.WriteEndObject();

                writer.WriteStartObject();
                writer.WritePropertyName("appliesTo");
                writer.WriteValue("teacher");

                writer.WritePropertyName("name");
                writer.WriteValue(profile.AzureDomain);
                writer.WriteEndObject();
                writer.WriteEndArray();

                writer.WriteEndObject();

                writer.WritePropertyName("licensesToAssign");
                writer.WriteStartArray();

                writer.WriteStartObject();
                writer.WritePropertyName("appliesTo");
                writer.WriteValue("teacher");

                writer.WritePropertyName("skuIds");
                writer.WriteStartArray();
                if (skus.Item1 != null)
                {
                    writer.WriteValue(skus.Item1);
                }
                writer.WriteEndArray();
                writer.WriteEndObject();

                writer.WriteStartObject();
                writer.WritePropertyName("appliesTo");
                writer.WriteValue("student");

                writer.WritePropertyName("skuIds");
                writer.WriteStartArray();
                if (skus.Item2 != null)
                {
                    writer.WriteValue(skus.Item2);
                }
                writer.WriteEndArray();
                writer.WriteEndObject();

                writer.WriteEndArray();

                writer.WriteEndObject();

                return(sb.ToString());
            }
        }