public async Task <HttpResponseMessage> Post([FromBody] AssociateDto associate)
        {
            try
            {
                var response = Request.CreateResponse(HttpStatusCode.OK, await logic.AddNewAssociate(associate));
                logger.Info("Add New Associate Successful");
                return(response);
            }

            catch (Exception ex)
            {
                LogHelper.ErrorLogger(logger, ex);
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
예제 #2
0
        public async Task <IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
        {
            //If the model is valid,
            //We will use it to create
            //a new instance of ApplicationUser
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //The user's credentials
            //By default, the user
            //will always be level 3
            var user = new ApplicationUser
            {
                UserName  = createUserModel.Username,
                Email     = createUserModel.Email,
                FirstName = createUserModel.FirstName,
                LastName  = createUserModel.LastName,
                Level     = 3,
                JoinDate  = DateTime.Now.Date
            };

            AssociateDto associate = new AssociateDto()
            {
                FirstName = createUserModel.FirstName,
                LastName  = createUserModel.LastName,
                Email     = createUserModel.Email,
                Gender    = createUserModel.Gender,
                //Batch = createUserModel.Batch
            };

            LogicHelper help = new LogicHelper();
            await help.AddNewAssociate(associate);

            //This will do the work for us to create the user
            //It will validate if the username or email has been used before
            //and will let us know if the matches the policy we have set forth
            //If the request is valid, the user will be added to our table in
            //the database and return a successful result
            IdentityResult addUserResult = await this.AppUserManager.CreateAsync(user, createUserModel.Password);


            //If the creation failed
            if (!addUserResult.Succeeded)
            {
                return(GetErrorResult(addUserResult));
            }

            //This will have the token that is valid for 6 hours only when we call this method
            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

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

            await this.AppUserManager.SendEmailAsync(user.Id, "Confirm Your Account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a> <br/> Your password is: " + AutoGenPassword.Generate(8, 10));

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

            //returns the created user
            return(Created(locationHeader, TheModelFactory.Create(user)));
        }