Exemplo n.º 1
0
    public async Task<IActionResult> RegisterUserAsync([FromBody] NewUserRequest request)
    {
        if (!ModelState.IsValid)
           { return BadRequest(new Resp<object>{ IsSuccess = false, Message = MessageErrorHelper.showModelStateError(ModelState) }); }

        var user  = await _userHelper.GetUserByEmailAsync(request.Correo);
        if (user != null)
           { return BadRequest(new Resp<object> { IsSuccess = false, Message = MessageCenter.appTextEmailUsed }); }

        var newUser = new UsuarioViewModel {  Correo    = request.Correo
                                            , CellPhone = request.CellPhone
                                            , FirstName = request.FirstName
                                            , LastName  = request.LastName
                                            , RoleId    = request.RoleId
                                            , Rol       = _combosHelper.GetComboRolesByValue(request.RoleId.ToString())
                                            , Password  = request.Password                                               
                                            , PasswordConfirm = request.PasswordConfirm                                               
                                            };

        var addUserResult  = await _userHelper.AddUsuario(newUser);         
        if (addUserResult != IdentityResult.Success)
           { return BadRequest(new Resp<object> { IsSuccess = false, Message = MessageErrorHelper.showIdentityResultError(addUserResult) }); }       

        var userLogin = await _userHelper.GetUserByEmailAsync(request.Correo);
        var myToken   = await _userHelper.GenerateEmailConfirmationTokenAsync(userLogin);
        var tokenLink = Url.Action("ConfirmEmail", "Account", new { userid = userLogin.Id,
                                                                    token  = myToken
                                                                  }, protocol: HttpContext.Request.Scheme);
            
        _mailHelper.SendEmailAccountConfirmation(request.Correo, tokenLink);
        
        return Ok(new Resp<object>{ IsSuccess = true, Message = MessageCenter.commonMessageEmailInst });

    }    
Exemplo n.º 2
0
        public async Task<IActionResult> Register(UsuarioViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var rol       = _combosHelper.GetComboRoles().Where(r => r.Value == viewModel.RoleId.ToString()).Select(r => r.Text);
                viewModel.Rol = rol.FirstOrDefault().ToString();

                var addUserResult = await _userHelper.AddUsuario(viewModel);                
                if (addUserResult == IdentityResult.Success)                
                    { 
                    var userLogin = await _userHelper.GetUserByEmailAsync(viewModel.Correo);
                    await _dataContext.Usuarios.AddAsync(new Usuario {  DatosFiscales = null, FirstName = viewModel.FirstName, LastName= viewModel.LastName, UserLogin = userLogin });
                    await _dataContext.SaveChangesAsync();
                    //ToDo: implementar el guardado en sesión del TOKEN
                    /*TokenResponse token = _userHelper.BuildToken(new LoginTARequest { Email = view.Correo, Password = view.Password, RememberMe = false });                    
                    var result = await _userHelper.LoginAsync(new LoginTARequest { Email =view.Correo, Password = view.Password, RememberMe= false });
                    if (result.Succeeded)
                       { return RedirectToAction("Index", "Home"); }
                    */
                    var defaultToken = await _userHelper.GenerateEmailConfirmationTokenAsync(userLogin);
                    var tokenLink = Url.Action("ConfirmEmail", "Account", new { userid = userLogin.Id,
                                                                                token = defaultToken
                                                                              }
                                                , protocol: HttpContext.Request.Scheme);

                    _mailHelper.SendEmailAccountConfirmation(viewModel.Correo, tokenLink);                        

                    ViewBag.Message = MessageCenter.commonMessageEmailInst;
                    
                     return View(viewModel);
                }

                ModelState.AddModelError(string.Empty, MessageErrorHelper.showIdentityResultError(addUserResult) );
                viewModel.Rol = rol.FirstOrDefault().ToString();
                return View(viewModel);

            }

            return View(viewModel);
        }