Example #1
0
        // GET: api/<ExtensionController>
        //[HttpGet("extensions/")]
        public async Task <IActionResult> GetExtensions()
        {
            List <ExtensionModel> retVal = new List <ExtensionModel>();

            try
            {
                var authenticationResult = GraphClientUtility.GetAuthentication();

                if (authenticationResult == null)
                {
                    _logger.LogError("ExtensionController-GetExtensions: Unable to get the Access token and Authentication Result");
                    return(NotFound());
                }
                _logger.LogInfo("ExtensionController-GetExtensions: [Started] to fetch user attribute in Azure AD B2C");

                var accessToken             = authenticationResult.Result.AccessToken;
                var b2cExtensionAppObjectId = GraphClientUtility.b2cExtensionAppObjectId;
                var tenantId           = GraphClientUtility.TenantId;
                var aadGraphVersion    = GraphClientUtility.AADGraphVersion;
                var aadGraphResourceId = GraphClientUtility.AADGraphResourceId;

                CustomExtension customExtensions = null;

                if (!string.IsNullOrEmpty(b2cExtensionAppObjectId) &&
                    !string.IsNullOrEmpty(tenantId) &&
                    !string.IsNullOrEmpty(aadGraphVersion) &&
                    !string.IsNullOrEmpty(aadGraphResourceId))
                {
                    string url = $"{aadGraphResourceId}{tenantId}" +
                                 $"{CareStreamConst.ForwardSlash}{CareStreamConst.Applications}{CareStreamConst.ForwardSlash}" +
                                 $"{b2cExtensionAppObjectId}{CareStreamConst.ForwardSlash}{CareStreamConst.ExtensionProperties}" +
                                 $"{CareStreamConst.Question}{aadGraphVersion}";

                    HttpClient         http    = new HttpClient();
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
                    request.Headers.Authorization = new AuthenticationHeaderValue(CareStreamConst.Bearer, accessToken);
                    HttpResponseMessage response = await http.SendAsync(request);

                    if (response.IsSuccessStatusCode)
                    {
                        var data = await response.Content.ReadAsStringAsync();

                        customExtensions = JsonConvert.DeserializeObject <CustomExtension>(data);
                    }
                }

                if (customExtensions != null)
                {
                    if (customExtensions.value != null)
                    {
                        _logger.LogInfo($"ExtensionController-GetExtensions:got {customExtensions.value.Count} user attribute from Azure AD B2C");
                    }

                    var b2cExtensionAppClientId = GraphClientUtility.b2cExtensionAppClientId;
                    b2cExtensionAppClientId = b2cExtensionAppClientId.Replace(CareStreamConst.Dash, "");
                    var toReplace = $"{CareStreamConst.Extension}{CareStreamConst.Underscore}{b2cExtensionAppClientId}{CareStreamConst.Underscore}";

                    foreach (var value in customExtensions.value)
                    {
                        try
                        {
                            var extensionModel = new ExtensionModel
                            {
                                ObjectId      = value.objectId,
                                DataType      = value.dataType,
                                TargetObjects = value.targetObjects,
                                AttributeType = CareStreamConst.Custom,
                                Description   = string.Empty,
                                Name          = value.name,
                                IsBuildIn     = false
                            };

                            if (!string.IsNullOrEmpty(extensionModel.Name))
                            {
                                extensionModel.Name = extensionModel.Name.Replace(toReplace, "");
                            }

                            retVal.Add(extensionModel);
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError($"ExtensionController-GetExtensions: fail to add extension [name:{value.name}] to collection ");
                            _logger.LogError(ex);
                        }
                    }
                }
                if (retVal.Any())
                {
                    retVal = retVal.OrderBy(x => x.Name).ToList();
                }

                _logger.LogInfo("ExtensionController-GetExtensions: [Completed] getting user attribute in Azure AD B2C");
            }
            catch (ServiceException ex)
            {
                _logger.LogError("ExtensionController-GetExtensions: Exception occured....");
                _logger.LogError(ex);
            }

            return(Ok(retVal));
        }
        public async Task <string> Post([FromBody] ExtensionModel extension)
        {
            try
            {
                //Check Null condition and add logging
                _logger.LogInfo("ExtensionController-Post: [Started] creation of user attribute in Azure AD B2C");

                if (extension == null)
                {
                    _logger.LogError("ExtensionController-Post: Input cannot be null");
                    return(string.Empty);
                }

                var json = JsonConvert.SerializeObject(extension);

                var authenticationResult = GraphClientUtility.GetAuthentication();

                if (authenticationResult == null)
                {
                    _logger.LogError("ExtensionController-Post: Unable to get the Access token and Authentication Result");
                    return(string.Empty);
                }
                var accessToken = authenticationResult.Result.AccessToken;

                var tenantId = GraphClientUtility.TenantId;
                var api      = $"{CareStreamConst.ForwardSlash}{CareStreamConst.Applications}{CareStreamConst.ForwardSlash}{GraphClientUtility.b2cExtensionAppClientId}{CareStreamConst.ForwardSlash}{CareStreamConst.ExtensionProperties}";

                HttpClient httpClient = new HttpClient();
                string     url        = GraphClientUtility.AADGraphResourceId + tenantId + api + "?" + GraphClientUtility.AADGraphVersion;

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
                request.Headers.Authorization = new AuthenticationHeaderValue(CareStreamConst.Bearer, accessToken);
                request.Content = new StringContent(json, Encoding.UTF8, $"{CareStreamConst.Application}{CareStreamConst.ForwardSlash}{CareStreamConst.Json}");
                HttpResponseMessage response = await httpClient.SendAsync(request);


                if (!response.IsSuccessStatusCode)
                {
                    string error = await response.Content.ReadAsStringAsync();

                    object formatted = JsonConvert.DeserializeObject(error);

                    var errorMessage = "Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented);

                    _logger.LogError($"ExtensionController-Post: {errorMessage}");
                    return(errorMessage);
                }


                _logger.LogInfo("ExtensionController-Post: [Completed] creation of user attribute in Azure AD B2C");


                return(await response.Content.ReadAsStringAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError("ExtensionController-Post: Exception occured....");
                _logger.LogError(ex);
                throw ex;
            }
        }