Exemplo n.º 1
0
        public async Task <IActionResult> EditUser(string id, UserRegisterInformation userRegisterInformation)
        {
            //fetch the user
            var user = await userManager.FindByIdAsync(id);

            //update user information
            user.Email       = userRegisterInformation.E_mail;
            user.FullName    = userRegisterInformation.FullName;
            user.PhoneNumber = userRegisterInformation.PhoneNumber;
            user.UserName    = userRegisterInformation.UserName;

            //check if the role changed or not
            var testAssign = await userManager.IsInRoleAsync(user, userRegisterInformation.Role);

            if (!testAssign)
            {
                //add new role to the user
                await userManager.AddToRoleAsync(user, userRegisterInformation.Role);
            }

            //update user in database
            var result = await userManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                return(Ok(user));
            }
            else
            {
                return(BadRequest());
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register(UserRegisterInformation userRegisterInformation)
        {
            //try
            //{
            //check if the email is in use
            var user1 = await userManager.FindByEmailAsync(userRegisterInformation.E_mail);

            if (user1 == null)
            {
                var user = new ExtendedUser
                {
                    UserName    = userRegisterInformation.UserName,
                    Email       = userRegisterInformation.E_mail,
                    FullName    = userRegisterInformation.FullName,
                    PhoneNumber = userRegisterInformation.PhoneNumber
                };
                var result = await userManager.CreateAsync(user, userRegisterInformation.Password);

                if (result.Succeeded)
                {
                    //var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var confirmationlink = Url.Action("ConfirmEmail", "Auth", new { userId = user.Id, Token = token }, Request.Scheme);

                    //logger.Log(LogLevel.Warning, confirmationlink);
                    await userManager.AddToRoleAsync(user, userRegisterInformation.Role);

                    return(Ok("successful"));
                }
                else
                {
                    return(BadRequest());
                }
            }
            else
            {
                return(BadRequest("the email is in use !! try another email"));
            }

            //}
            //catch (Exception)
            //{
            //    return BadRequest();
            //}
        }