Exemple #1
0
 public UserResponse Create(ApplicationUser appUser)
 {
     return new UserResponse
     {
         Url = _UrlHelper.Link("GetUserById", new { id = appUser.Id }),
         Id = appUser.Id,
         UserName = appUser.UserName,
         FullName = string.Format("{0} {1}", appUser.FirstName, appUser.LastName),
         Email = appUser.Email,
         EmailConfirmed = appUser.EmailConfirmed,
         Level = appUser.Level,
         JoinDate = appUser.JoinDate,
         Roles = _AppUserManager.GetRolesAsync(appUser.Id).Result,
         Claims = _AppUserManager.GetClaimsAsync(appUser.Id).Result
     };
 }
        public async Task<IHttpActionResult> CreateUser(UserCreateRequest UserRequest)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new ApplicationUser()
            {
                UserName = UserRequest.Username,
                Email = UserRequest.Email,
                FirstName = UserRequest.FirstName,
                LastName = UserRequest.LastName,
                Level = 3,
                JoinDate = DateTime.Now.Date,
            };

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

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

            // Generacion del codigo para confirmacion.
            string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

            // Generacion del Link para confirmacion.
            var callbackUrl = new Uri(
                Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code })
                );
            try
            {
                // Envio de correo al usuario.
                await this.AppUserManager.SendEmailAsync(user.Id, "WebApi Rest Confirmacion", "Hemos recibido tu solicitud. Por favor confirma tu cuenta haciendo clic <a href=\"" + callbackUrl + "\">AQUI</a>");

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

                return Created(locationHeader, TheModelFactory.Create(user));
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }


        }