예제 #1
0
        public async Task <IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_repo.Exists(createUserModel.PhoneNumber))
            {
                return(BadRequest("Phone number is already taken"));
            }

            var user = new ApplicationUser()
            {
                UserName       = createUserModel.UserName,
                Email          = createUserModel.Email,
                CustomerInfoId = createUserModel.CustomerInfoId,
                JoinDate       = DateTime.Now.Date,
                PhoneNumber    = createUserModel.PhoneNumber,
                EmailConfirmed = true
            };

            IdentityResult addUserResult = await AppUserManager.CreateAsync(user, createUserModel.Password);

            if (addUserResult.Succeeded)
            {
                IdentityResult addRoleResult = await AppUserManager.AddToRoleAsync(user.Id, "Customer");

                if (!addRoleResult.Succeeded)
                {
                    return(GetErrorResult(addRoleResult));
                }
            }
            else
            {
                return(GetErrorResult(addUserResult));
            }

            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

            //TODO: Uncomment to send email confirmations
            //await this.AppUserManager.SendEmailAsync(user.Id, "Confirm your account"
            //, "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

            Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

            return(Created(locationHeader, TheModelFactory.Create(user)));
        }