Exemplo n.º 1
0
        public async Task <IActionResult> FindEmail([FromBody] FindEmailModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var person = await _pco.FindPersonByEmail(model.EmailAddress);

            if (person == null)
            {
                return(this.NotFound());
            }

            if (this.User.Identity.IsAuthenticated)
            {
                if (this.User.HasClaim(x => x.Type == ClaimTypes.Email && x.Value.Equals(model.EmailAddress, StringComparison.OrdinalIgnoreCase)))
                {
                    return(this.Ok(new {
                        Verified = true
                    }));
                }
            }

            var token = await _db.CreateLoginToken(model.EmailAddress, person.ID);

            await _messageService.SendMessageAsync(model.EmailAddress, "Your Verification Code", $"Please use the code {token.Token} to verify your identity with {_options.Name}.");

            return(this.Ok(new
            {
                TokenID = token.TokenID
            }));
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            try
            {
                log.LogInformation("Request started");
                string         requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                FindEmailModel data        = JsonConvert.DeserializeObject <FindEmailModel>(requestBody);
                log.LogInformation(requestBody);

                if (!String.IsNullOrEmpty(data.GivenName) && !String.IsNullOrEmpty(data.Surname) &&
                    !String.IsNullOrEmpty(data.Day) && !String.IsNullOrEmpty(data.Month) && !String.IsNullOrEmpty(data.Year) &&
                    !String.IsNullOrEmpty(data.PostalCode))
                {
                    string email = "";
                    using (var httpClient = new HttpClient())
                    {
                        var dob          = String.Format("{0}-{1}-{2}", data.Year, data.Month, data.Day);
                        var searchApiUrl = _appSettings.NcsDssSearchApiUrl;// Environment.GetEnvironmentVariable("ncsdsssearchapiurl", EnvironmentVariableTarget.Process);
                        var url          = String.Format("{0}?&search=GivenName:{1} FamilyName:{2} PostCode={3}&filter=DateOfBirth eq {4}",
                                                         searchApiUrl, data.GivenName, data.Surname, data.PostalCode, dob);
                        using (var request = new HttpRequestMessage(new HttpMethod("GET"), url))
                        {
                            request.Headers.TryAddWithoutValidation("api-key", _appSettings.NcsDssApiKey);                             // Environment.GetEnvironmentVariable("ncsdssapikey", EnvironmentVariableTarget.Process));
                            request.Headers.TryAddWithoutValidation("version", _appSettings.NcsDssSearchApiVersion);                   //Environment.GetEnvironmentVariable("ncsdsssearchapiversion", EnvironmentVariableTarget.Process));
                            request.Headers.TryAddWithoutValidation("Ocp-Apim-Subscription-Key", _appSettings.OcpApimSubscriptionKey); //Environment.GetEnvironmentVariable("OcpApimSubscriptionKey", EnvironmentVariableTarget.Process));

                            var response = await httpClient.SendAsync(request);

                            if (response.StatusCode == System.Net.HttpStatusCode.OK)
                            {
                                var result = JsonConvert.DeserializeObject <SearchAPIResponseModel>(await response.Content.ReadAsStringAsync());
                                if (result.Value.Length > 0)
                                {
                                    email = result.Value[0].EmailAddress != null ? result.Value[0].EmailAddress.ToString() : "";
                                }
                            }
                            else
                            {
                                return(new BadRequestObjectResult(new ResponseContentModel
                                {
                                    userMessage = "Sorry, Something happened unexpectedly. Please try after sometime.",
                                    status = 400
                                }));
                            }
                        }
                    }
                    if (!String.IsNullOrEmpty(email))
                    {
                        return(new OkObjectResult(new EmailFoundResponseModel
                        {
                            foundEmail = email,
                            isFound = true
                        }));
                    }
                    else
                    {
                        return(new OkObjectResult(new EmailFoundResponseModel
                        {
                            foundEmail = "",
                            isFound = false
                        }));
                    }
                }
                else
                {
                    return(new OkObjectResult(new EmailFoundResponseModel
                    {
                        foundEmail = "",
                        isFound = false
                    }));
                }
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.ToString());

                return(new BadRequestObjectResult(new ResponseContentModel
                {
                    userMessage = "Sorry, Something happened unexpectedly. Please try after sometime.",
                    status = 400
                }));
            }
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            try
            {
                log.LogInformation("Request started");
                string         requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                FindEmailModel data        = JsonConvert.DeserializeObject <FindEmailModel>(requestBody);
                log.LogInformation(requestBody);

                using (var httpClient = new HttpClient())
                {
                    var searchApiUrl = _appSettings.NcsDssSearchApiUrl;
                    var url          = String.Format("{0}?&search=EmailAddress:{1}", searchApiUrl, data.Email);
                    using (var request = new HttpRequestMessage(new HttpMethod("GET"), url))
                    {
                        request.Headers.TryAddWithoutValidation("api-key", _appSettings.NcsDssApiKey);
                        request.Headers.TryAddWithoutValidation("version", _appSettings.NcsDssSearchApiVersion);
                        request.Headers.TryAddWithoutValidation("Ocp-Apim-Subscription-Key", _appSettings.OcpApimSubscriptionKey);
                        request.Headers.TryAddWithoutValidation("TouchpointId", _appSettings.TouchpointId.ToString());

                        var response = await httpClient.SendAsync(request);

                        var retryConter = Convert.ToInt32(data.RetryCounter);
                        ++retryConter;
                        if (response.StatusCode == System.Net.HttpStatusCode.OK)
                        {
                            var result = JsonConvert.DeserializeObject <SearchAPIResponseModel>(await response.Content.ReadAsStringAsync());
                            var value  = result.Value.FirstOrDefault(p => p.EmailAddress.ToString().ToLower() == data.Email.ToLower());
                            if (value != null)
                            {
                                return(new OkObjectResult(new ResponseContentModel
                                {
                                    version = "1.0.0",
                                    status = 200,
                                    isFound = true
                                }));
                            }
                            else
                            {
                                return(new OkObjectResult(new ResponseContentModel
                                {
                                    version = "1.0.0",
                                    status = 200,
                                    isFound = false
                                }));
                            }
                        }
                        else
                        {
                            return(new BadRequestObjectResult(new ResponseContentModel
                            {
                                version = "1.0.0",
                                userMessage = "Sorry, Something happened unexpectedly. Please try after sometime.",
                                status = 400,
                            }));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.ToString());

                return(new BadRequestObjectResult(new ResponseContentModel
                {
                    version = "1.0.0",
                    userMessage = "Sorry, Something happened unexpectedly. Please try after sometime.",
                    developerMessage = "See logging provider failure dependencies for exception information.",
                    status = 400
                }));
            }
        }