Beispiel #1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "users/{id}")]
            [RequestBodyType(typeof(User), "User")]
            HttpRequest req, string id, ILogger log, ExecutionContext context)
        {
            log.LogInformation($"{context?.FunctionName} processed a HTTP request.");
            // TelemetryClient.Context.Operation.Id = context?.InvocationId.ToString(); // No longer needed?

            string requestBody = new StreamReader(req.Body).ReadToEnd();
            var    user        = JsonConvert.DeserializeObject <User>(requestBody);

            if (user is null)
            {
                return(new BadRequestObjectResult("No User definition specified in body."));
            }

            user.Id = user.Id ?? id;

            if (user.Id != id)
            {
                return(new BadRequestObjectResult("User Id provided in request JSON does not match the Id provided in the route."));
            }

            try
            {
                var query    = GremlinHelper.UpdateVertexQuery(id, user, log);
                var response = new GraphResponse(await GremlinClient.SubmitAsync <dynamic>(query));

                GremlinHelper.ThrowIfResponseInvalid(response);

                GremlinHelper.GraphTelemetryEvent(TelemetryClient, "GraphVertexUpdate", response, "vertex", "user");

                if (response.Entities == null || response.Entities.Count() < 1)
                {
                    return(new NotFoundResult());
                }

                user = response.GetEntityAsType <User>();
            }
            catch (ResponseException ex)
            {
                GremlinHelper.HandleGraphResponseException(ex, log, context, TelemetryClient);
            }
            catch (Exception ex)
            {
                GremlinHelper.HandleGeneralException(ex, log, context, TelemetryClient);
            }

            return(user != null ?
                   new OkObjectResult(user)
            {
                StatusCode = 200
            } :
                   new OkObjectResult("Failed to update user.")
            {
                StatusCode = 500
            });
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "users/password/{id}")]
            [RequestBodyType(typeof(string), "Password")]
            HttpRequest req, string id, ILogger log, ExecutionContext context)
        {
            log.LogInformation($"{context?.FunctionName} processed a HTTP request.");
            // TelemetryClient.Context.Operation.Id = context?.InvocationId.ToString(); // No longer needed?

            string password = new StreamReader(req.Body).ReadToEnd();

            if (string.IsNullOrWhiteSpace(password))
            {
                return(new BadRequestObjectResult("No password value supplied in body."));
            }

            try
            {
                // FIND USER IN GRAPH

                var query    = GremlinHelper.GetVertexQuery <User>(id);
                var response = new GraphResponse(await GremlinClient.SubmitAsync <dynamic>(query));

                GremlinHelper.ThrowIfResponseInvalid(response);

                if (response.Entities == null || response.Entities.Count() < 1)
                {
                    return(new NotFoundResult());
                }

                GremlinHelper.GraphTelemetryEvent(TelemetryClient, "GraphVertexRetrieve_ChangePassword", response, "vertex", "user");

                var user = response.GetEntityAsType <User>();


                // CHANGE PASSWORD ON B2C

                try
                {
                    var passwordProfile = new B2CPasswordProfile()
                    {
                        Password = password,
                        ForceChangePasswordNextLogin = false
                    };
                    dynamic wrapper = new ExpandoObject()
                    {
                    };
                    wrapper.passwordProfile = passwordProfile;

                    var b2cResponse = await B2CGraphClient.UpdateUser(user.IdentityId, JsonConvert.SerializeObject(wrapper), log);
                }
                catch (B2CPasswordComplexityException)
                {
                    return(new BadRequestObjectResult("The specified password does not comply with password complexity requirements. Please provide a different password."));
                }
                catch (Exception ex)
                {
                    TelemetryClient.TrackException(ex, new Dictionary <string, string>()
                    {
                        { "userId", id }
                    }, null);
                    log.LogError($"{context?.FunctionName} Error: {ex.Message}");

                    return(new OkObjectResult("Error with identity provider update.")
                    {
                        StatusCode = 500
                    });
                }

                if (user.CustomProperties.ContainsKey("PasswordAutoGenerated"))
                {
                    query    = GremlinHelper.UpdateVertexQuery(id, user, log);
                    response = new GraphResponse(await GremlinClient.SubmitAsync <dynamic>(query));

                    GremlinHelper.ThrowIfResponseInvalid(response);

                    GremlinHelper.GraphTelemetryEvent(TelemetryClient, "GraphVertexUpdate_PasswordChange", response, "vertex", "user");
                }
            }
            catch (ResponseException ex)
            {
                GremlinHelper.HandleGraphResponseException(ex, log, context, TelemetryClient);
            }
            catch (Exception ex)
            {
                GremlinHelper.HandleGeneralException(ex, log, context, TelemetryClient);
            }

            return(new OkObjectResult(null)
            {
                StatusCode = 204
            });
        }