Exemple #1
0
 public UserReturnModel Create(ApplicationUser appUser)
 {
     return new UserReturnModel
     {
         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(CreateUserBindingModel createUserModel)
        {            

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var user = new ApplicationUser()
                {
                    UserName = createUserModel.Username,
                    Email = createUserModel.Email,
                    FirstName = createUserModel.FirstName,
                    LastName = createUserModel.LastName,
                    Level = 3,
                    JoinDate = DateTime.Now.Date,
                    Liga = new Liga { // fpaz: creo la nueva liga, asociada a la cuenta que se esta creando, con los valores iniciales
                        Nombre="Nueva Liga"

                    }
                };

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

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

                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>");

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

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