Ejemplo n.º 1
0
        public async Task <IHttpActionResult> PostUsers(UserModel model)
        {
            try
            {
                _logger.Debug(string.Format("ini process - Post,idUser:{0}", CurrentIdUser));

                if (!ModelState.IsValid)
                {
                    _logger.Debug(string.Format("ini Post - inValid,idUser:{0}", CurrentIdUser));
                    return(BadRequest(ModelState));
                }

                User user = AutoMapper.Mapper.Map <User>(model);

                _logger.Debug(string.Format("ini create ,idUser:{0}", CurrentIdUser));

                IdentityResult addUserResult = _appUserManager.Create(user, model.Password);

                if (!addUserResult.Succeeded)
                {
                    _logger.Debug(string.Format("ini Post - GetErrorResult,idUser:{0}", CurrentIdUser));
                    return(GetErrorResult(addUserResult));
                }

                _logger.Debug(string.Format("user Post info email:{0}, idUser:{1}", user.Email, user.Id));

                Role role = _roleBL.Get(model.IdRole);

                _logger.Debug(string.Format("user Post info role:{0}, idUser:{1}", role.Name, user.Id));

                _appUserManager.AddToRole(user.Id, role.Name);
                _appUserManager.AddClaim(user.Id, new Claim(ClaimTypes.Authentication, "local"));

                string generatedCode = await _appUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                string callbackUrl = string.Format("{0}/{1}/{2}", model.ConfirmURL, user.Id, System.Web.HttpUtility.UrlEncode(generatedCode));

                string bodyHtmlEmail = Helpers.CreateBodyEmail(user, callbackUrl);

                await _appUserManager.SendEmailAsync(user.Id, "Confirm your account", bodyHtmlEmail);

                _logger.Debug(string.Format("finish create ,idUser:{0},newIdUser{1}", CurrentIdUser, user.Id));

                return(Ok(new JsonResponse {
                    Success = true, Message = "User was Saved successfully", Data = user
                }));
            }
            catch (Exception ex)
            {
                LogError(ex);
                return(InternalServerError(ex));
            }
        }
Ejemplo n.º 2
0
        public IHttpActionResult GetRoles(int id)
        {
            try
            {
                _logger.Debug(string.Format("ini process - GetModel,idUser:{0}", CurrentIdUser));

                Role role = _roleBL.Get(id);

                _logger.Debug(string.Format("finish GetModel - success,idUser:{0}, idRole{1}", CurrentIdUser, id));
                return(Ok(role ?? new Role()));
            }
            catch (Exception ex)
            {
                LogError(ex);
                return(InternalServerError(ex));
            }
        }
Ejemplo n.º 3
0
        public Task <TRole> FindByIdAsync(int idRole)
        {
            TRole result = _roleBL.Get(idRole) as TRole;

            return(Task.FromResult <TRole>(result));
        }