/// <summary> /// Retrieves an access token via OAuth 2.0 protocol. /// </summary> /// <param name="apiIntegration"></param> /// <returns></returns> private static async Task <string> GetOauth2TokenAsync(ApiIntegrationElement apiIntegration) { try { using (var httpClient = new System.Net.Http.HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String( Encoding.ASCII.GetBytes( $"{apiIntegration.Username}:{apiIntegration.Password}"))); var body = "grant_type=client_credentials"; StringContent theContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); HttpResponseMessage response = await httpClient.PostAsync(new Uri(apiIntegration.TokenService), theContent).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var responseJson = await response.Content.ReadAsStringAsync(); return((string)JObject.Parse(responseJson)["access_token"]); } else { throw new Exception("Invalid response"); } } } catch (Exception ex) { logger.ErrorFormat("DataService GetOauth2TokenAsync Error: {0}", ex); throw; } }
/// <summary> /// Retrieves an access token via OAuth WRAP protocol. /// </summary> /// <param name="apiIntegration"></param> /// <returns></returns> private static async Task <string> GetOauthWrapTokenAsync(ApiIntegrationElement apiIntegration) { try { // Make the Web API call using (var client = new System.Net.Http.HttpClient()) { // Build the form body that will be posted var body = string.Format("wrap_name={0}&wrap_password={1}&wrap_scope={2}", HttpUtility.UrlEncode(apiIntegration.Username), HttpUtility.UrlEncode(apiIntegration.Password), apiIntegration.Root); StringContent theContent = new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded"); // Call the API and post the form data to request the token HttpResponseMessage response = await client.PostAsync(new Uri(apiIntegration.TokenService), theContent).ConfigureAwait(false); //ConfigureAwait is required when use with ASP.NET because of SynchronizationContext. if (response.IsSuccessStatusCode) { // Pull the content of the response, decode the response, strip off certain parts of the access token and return just the token part for use var accessToken = await response.Content.ReadAsStringAsync(); var wrapAccessPart = HttpUtility.UrlDecode(accessToken).Split('&').FirstOrDefault(x => x.Contains("wrap_access_token_expires_in")); return(HttpUtility.UrlDecode(accessToken).Replace("wrap_access_token=", string.Empty).Replace("&" + wrapAccessPart, string.Empty)); } else { // Handle invalid responses here throw new Exception("Invalid Response."); } } } catch (Exception ex) { logger.ErrorFormat("DataService GetOauthWrapTokenAsync Error: {0}", ex); throw; } }
public HttpResponseMessage Configurations(ConfigurationModel configurationModel) { try { Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath); CampusLogicSection campusLogicSection = (CampusLogicSection)config.GetSection("campusLogicConfig"); SmtpSection smtpSection = (SmtpSection)config.GetSection("system.net/mailSettings/smtp"); AppSettingsSection appSettings = config.AppSettings; //Workarounds until we can figure out why the properties won't deserialize as is campusLogicSection.EventNotifications = configurationModel.CampusLogicSection.EventNotifications; campusLogicSection.EventNotifications.EventNotificationsEnabled = configurationModel.CampusLogicSection.EventNotificationsEnabled; foreach (EventNotificationHandler eventNotificationHandler in configurationModel.CampusLogicSection.EventNotificationsList) { campusLogicSection.EventNotifications.Add(eventNotificationHandler); } campusLogicSection.FileStoreSettings = configurationModel.CampusLogicSection.FileStoreSettings; foreach (FieldMapSettings fieldMapSetting in configurationModel.CampusLogicSection.FileStoreSettings.FileStoreMappingCollection) { campusLogicSection.FileStoreSettings.FileStoreMappingCollectionConfig.Add(fieldMapSetting); } campusLogicSection.DocumentSettings = configurationModel.CampusLogicSection.DocumentSettings; foreach (FieldMapSettings fieldMapSetting in configurationModel.CampusLogicSection.DocumentSettings.FieldMappingCollection) { campusLogicSection.DocumentSettings.FieldMappingCollectionConfig.Add(fieldMapSetting); } campusLogicSection.StoredProcedures = configurationModel.CampusLogicSection.StoredProcedures; campusLogicSection.StoredProcedures.StoredProceduresEnabled = configurationModel.CampusLogicSection.StoredProceduresEnabled; foreach (StoredProcedureDto storedProcedure in configurationModel.CampusLogicSection.StoredProcedureList) { StoredProcedureElement storedProcedureElement = new StoredProcedureElement(); storedProcedureElement.Name = storedProcedure.Name; foreach (ParameterDto parameter in storedProcedure.ParameterList) { ParameterElement parameterElement = new ParameterElement(); parameterElement.Name = parameter.Name; parameterElement.DataType = parameter.DataType; parameterElement.Length = parameter.Length; parameterElement.Source = parameter.Source; storedProcedureElement.Add(parameterElement); } campusLogicSection.StoredProcedures.Add(storedProcedureElement); } campusLogicSection.BatchProcessingTypes = configurationModel.CampusLogicSection.BatchProcessingTypes; campusLogicSection.BatchProcessingTypes.BatchProcessingEnabled = configurationModel.CampusLogicSection.BatchProcessingEnabled; foreach (BatchProcessingTypeDto batchProcessingType in configurationModel.CampusLogicSection.BatchProcessingTypesList) { BatchProcessingTypeElement batchProcessingTypeElement = new BatchProcessingTypeElement(); batchProcessingTypeElement.TypeName = batchProcessingType.TypeName; foreach (BatchProcessDto batchProcess in batchProcessingType.BatchProcesses) { var batchProcessElement = new BatchProcessElement(); batchProcessElement.BatchName = batchProcess.BatchName; if (batchProcessingTypeElement.TypeName == ConfigConstants.AwardLetterPrintBatchType) { batchProcessElement.MaxBatchSize = batchProcess.MaxBatchSize; batchProcessElement.FilePath = batchProcess.FilePath; batchProcessElement.FileNameFormat = batchProcess.FileNameFormat; batchProcessElement.BatchExecutionMinutes = batchProcess.BatchExecutionMinutes; batchProcessElement.IndexFileEnabled = batchProcess.IndexFileEnabled; batchProcessElement.FileDefinitionName = batchProcess.FileDefinitionName; } batchProcessingTypeElement.Add(batchProcessElement); } campusLogicSection.BatchProcessingTypes.Add(batchProcessingTypeElement); } campusLogicSection.FileDefinitionSettings = configurationModel.CampusLogicSection.FileDefinitionSettings; campusLogicSection.FileDefinitionSettings.FileDefinitionsEnabled = configurationModel.CampusLogicSection.FileDefinitionsEnabled; foreach (var listSetting in configurationModel.CampusLogicSection.FileDefinitionsList) { FileDefinitionSetting fileDefinitionSetting = new FileDefinitionSetting(); fileDefinitionSetting.Name = listSetting.Name; fileDefinitionSetting.FileNameFormat = listSetting.FileNameFormat; fileDefinitionSetting.IncludeHeaderRecord = listSetting.IncludeHeaderRecord; fileDefinitionSetting.FileExtension = listSetting.FileExtension; fileDefinitionSetting.FileFormat = listSetting.FileFormat; foreach (var fieldMapping in listSetting.FieldMappingCollection) { fileDefinitionSetting.FieldMappingCollectionConfig.Add(fieldMapping); } campusLogicSection.FileDefinitionSettings.Add(fileDefinitionSetting); } campusLogicSection.ApiIntegrations = configurationModel.CampusLogicSection.ApiIntegrations; campusLogicSection.ApiIntegrations.ApiIntegrationsEnabled = configurationModel.CampusLogicSection.ApiIntegrationsEnabled; foreach (ApiIntegrationDto apiIntegration in configurationModel.CampusLogicSection.ApiIntegrationsList) { ApiIntegrationElement apiIntegrationElement = new ApiIntegrationElement(); apiIntegrationElement.ApiId = apiIntegration.ApiId; apiIntegrationElement.ApiName = apiIntegration.ApiName; apiIntegrationElement.Authentication = apiIntegration.Authentication; apiIntegrationElement.TokenService = apiIntegration.TokenService; apiIntegrationElement.Root = apiIntegration.Root; apiIntegrationElement.Username = apiIntegration.Username; apiIntegrationElement.Password = apiIntegration.Password; campusLogicSection.ApiIntegrations.Add(apiIntegrationElement); } campusLogicSection.ApiEndpoints = configurationModel.CampusLogicSection.ApiEndpoints; foreach (ApiIntegrationEndpointDto apiEndpoint in configurationModel.CampusLogicSection.ApiEndpointsList) { ApiIntegrationEndpointElement endpointElement = new ApiIntegrationEndpointElement(); endpointElement.Name = apiEndpoint.Name; endpointElement.Endpoint = apiEndpoint.Endpoint; endpointElement.ApiId = apiEndpoint.ApiId; endpointElement.Method = apiEndpoint.Method; endpointElement.MimeType = apiEndpoint.MimeType; endpointElement.ParameterMappings = JArray.Parse(apiEndpoint.ParameterMappings).ToString(); campusLogicSection.ApiEndpoints.Add(endpointElement); } campusLogicSection.PowerFaidsSettings = configurationModel.CampusLogicSection.PowerFaidsSettings; campusLogicSection.PowerFaidsSettings.PowerFaidsEnabled = configurationModel.CampusLogicSection.PowerFaidsEnabled; foreach (var record in configurationModel.CampusLogicSection.PowerFaidsList) { PowerFaidsSetting powerFaidsSetting = new PowerFaidsSetting(); powerFaidsSetting.Event = record.Event; powerFaidsSetting.Outcome = record.Outcome; powerFaidsSetting.ShortName = record.ShortName; powerFaidsSetting.RequiredFor = record.RequiredFor; powerFaidsSetting.Status = record.Status; powerFaidsSetting.DocumentLock = record.DocumentLock; powerFaidsSetting.VerificationOutcome = record.VerificationOutcome; powerFaidsSetting.VerificationOutcomeLock = record.VerificationOutcomeLock; powerFaidsSetting.TransactionCategory = record.TransactionCategory; campusLogicSection.PowerFaidsSettings.PowerFaidsSettingCollectionConfig.Add(powerFaidsSetting); } campusLogicSection.BulkActionSettings = configurationModel.CampusLogicSection.BulkActionSettings; campusLogicSection.ISIRUploadSettings = configurationModel.CampusLogicSection.ISIRUploadSettings; campusLogicSection.ISIRCorrectionsSettings = configurationModel.CampusLogicSection.ISIRCorrectionsSettings; campusLogicSection.AwardLetterUploadSettings = configurationModel.CampusLogicSection.AwardLetterUploadSettings; campusLogicSection.DataFileUploadSettings = configurationModel.CampusLogicSection.DataFileUploadSettings; campusLogicSection.FileMappingUploadSettings = configurationModel.CampusLogicSection.FileMappingUploadSettings; campusLogicSection.AwardLetterPrintSettings = configurationModel.CampusLogicSection.AwardLetterPrintSettings; campusLogicSection.SMTPSettings = configurationModel.CampusLogicSection.SMTPSettings; campusLogicSection.ClientDatabaseConnection = configurationModel.CampusLogicSection.ClientDatabaseConnection; campusLogicSection.DocumentImportSettings = configurationModel.CampusLogicSection.DocumentImportSettings; smtpSection.DeliveryMethod = configurationModel.SmtpSection.DeliveryMethod; smtpSection.DeliveryFormat = configurationModel.SmtpSection.DeliveryFormat; smtpSection.From = configurationModel.SmtpSection.From; smtpSection.Network.ClientDomain = configurationModel.SmtpSection.Network.ClientDomain; smtpSection.Network.DefaultCredentials = configurationModel.SmtpSection.Network.DefaultCredentials; smtpSection.Network.EnableSsl = configurationModel.SmtpSection.Network.EnableSsl; smtpSection.Network.Host = configurationModel.SmtpSection.Network.Host; smtpSection.Network.Password = configurationModel.SmtpSection.Network.Password; smtpSection.Network.Port = configurationModel.SmtpSection.Network.Port; smtpSection.Network.TargetName = configurationModel.SmtpSection.Network.TargetName; smtpSection.Network.UserName = configurationModel.SmtpSection.Network.UserName; smtpSection.SpecifiedPickupDirectory.PickupDirectoryLocation = configurationModel.SmtpSection.SpecifiedPickupDirectory.PickupDirectoryLocation; appSettings.Settings.Clear(); foreach (var appSetting in configurationModel.AppSettingsSection) { appSettings.Settings.Add(FirstCharToUpper(appSetting.Key), appSetting.Value); } Assembly clConnectAssembly = Assembly.GetExecutingAssembly(); AssemblyName clConnect = clConnectAssembly.GetName(); appSettings.Settings["ClConnectVersion"].Value = clConnect.Version.ToString(); appSettings.Settings["Webpages:Version"].Value = "2.0.0.0"; appSettings.Settings["Webpages:Enabled"].Value = "false"; appSettings.Settings["PreserveLoginUrl"].Value = "true"; appSettings.Settings["ClientValidationEnabled"].Value = "true"; appSettings.Settings["UnobtrusiveJavaScriptEnabled"].Value = "true"; if (appSettings.Settings["Environment"].Value == EnvironmentConstants.SANDBOX) { appSettings.Settings["StsUrl"].Value = ApiUrlConstants.STS_URL_SANDBOX; appSettings.Settings["SvWebApiUrl"].Value = ApiUrlConstants.SV_API_URL_SANDBOX; appSettings.Settings["AwardLetterWebApiUrl"].Value = ApiUrlConstants.AL_API_URL_SANDBOX; appSettings.Settings["PmWebApiUrl"].Value = ApiUrlConstants.PM_API_URL_SANDBOX; appSettings.Settings["SuWebApiUrl"].Value = ApiUrlConstants.SU_API_URL_SANDBOX; } else { appSettings.Settings["StsUrl"].Value = ApiUrlConstants.STS_URL_PRODUCTION; appSettings.Settings["SvWebApiUrl"].Value = ApiUrlConstants.SV_API_URL_PRODUCTION; appSettings.Settings["AwardLetterWebApiUrl"].Value = ApiUrlConstants.AL_API_URL_PRODUCTION; appSettings.Settings["PmWebApiUrl"].Value = ApiUrlConstants.PM_API_URL_PRODUCTION; appSettings.Settings["SuWebApiUrl"].Value = ApiUrlConstants.SU_API_URL_PRODUCTION; } ClearOldFileDefinitions(campusLogicSection); config.Save(); return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { logger.ErrorFormat("SetupController SaveConfigurations Error: {0}", ex); return(Request.CreateResponse(HttpStatusCode.ExpectationFailed)); } }