/// <summary>
        /// Create consumer user accounts
        /// When creating user accounts in a B2C tenant, you can send an HTTP POST request to the /users endpoint
        /// </summary>
        public async Task <bool> UpdateAccount(string objectId,
                                               string city)
        {
            if (string.IsNullOrEmpty(objectId))
            {
                throw new Exception("You must provide user's objectId");
            }

            try
            {
                // Create Graph json string from object
                GraphAccountModel graphUserModel = new GraphAccountModel()
                {
                    City = city
                };

                // Send the json to Graph API end point
                await SendGraphRequest($"/users/{objectId}", null, graphUserModel.ToString(), new HttpMethod("PATCH"));

                return(true);
            }
            catch (Exception ex)
            {
                // TBD: Error handling
                throw;
            }
        }
Пример #2
0
        public async Task <bool> UpdateAccount(
            string objectId,
            string userType,
            string signInName,
            string issuer,
            string issuerUserId,
            string email,
            string password,
            string displayName,
            string givenName,
            string surname,
            bool isActivated)
        {
            if (string.IsNullOrEmpty(signInName) && string.IsNullOrEmpty(issuerUserId))
            {
                throw new Exception("You must provide user's signInName or issuerUserId");
            }

            if (string.IsNullOrEmpty(displayName) || displayName.Length < 1)
            {
                throw new Exception("Dispay name is NULL or empty, you must provide valid dislay name");
            }

            try
            {
                // Create Graph json string from object
                GraphAccountModel graphUserModel = new GraphAccountModel(
                    Tenant,
                    userType,
                    signInName,
                    issuer,
                    issuerUserId,
                    email,
                    password,
                    displayName,
                    givenName,
                    surname);

                graphUserModel.objectId       = objectId;
                graphUserModel.accountEnabled = isActivated;


                // Send the json to Graph API end point
                await SendGraphRequest(string.Format("/users/{0}", graphUserModel.objectId), null, graphUserModel.ToString(), new HttpMethod("PATCH"));

                Console.WriteLine($"Azure AD user account '{displayName}' updated");

                return(true);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("ObjectConflict"))
                {
                    if (ex.Message.Contains("signInNames "))
                    {
                        // TBD: Chnage to trace
                        Console.WriteLine($"User with same signInNames '{signInName}' already exists in Azure AD");
                    }
                    else if (ex.Message.Contains("userIdentities "))
                    {
                        // TBD: Chnage to trace
                        Console.WriteLine($"User with same userIdentities '{issuerUserId}' already exists in Azure AD");
                    }
                    else if (ex.Message.Contains("one or more"))
                    {
                        // TBD: Chnage to trace
                        Console.WriteLine($"User with same userIdentities '{issuerUserId}', and signInNames '{signInName}'  already exists in Azure AD");
                    }
                }

                return(false);
            }
        }