Ejemplo n.º 1
0
        public async Task <JsonResult> Register(UserAddVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    User entity = _mapper.Map <UserAddVM, User>(model);
                    entity.Id                = Guid.NewGuid();
                    entity.CreateDateTime    = DateTime.UtcNow;
                    entity.LastLoginDateTime = DateTime.UtcNow;
                    var identityResult = await _userManager.CreateAsync(entity, model.PasswordHash);

                    if (identityResult.Succeeded)
                    {
                        await _signInManager.SignInAsync(entity, isPersistent : false);


                        UserAuthenticateVM returnVM = new UserAuthenticateVM();
                        returnVM       = _mapper.Map <User, UserAuthenticateVM>(entity);
                        returnVM.Token = GetToken(entity);

                        return(new JsonResult(returnVM));
                    }
                    else
                    {
                        return(new JsonResult(APIResult.CreateVMWithRec <object>(identityResult.Errors)));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new JsonResult(APIResult.CreateVMWithError(ex, APIResult.CreateVM(false, null, AppStatusCode.ERR01001))));
            }
            return(new JsonResult(APIResult.CreateVM(false, null, AppStatusCode.ERR01001)));
        }
Ejemplo n.º 2
0
        public async Task <JsonResult> CreateToken(UserLoginVM model)
        {
            var loginResult = await _signInManager.PasswordSignInAsync(model.UserName, model.PasswordHash, isPersistent : false, lockoutOnFailure : false);

            if (!loginResult.Succeeded)
            {
                return(new JsonResult(APIResult.CreateVM(false, null, AppStatusCode.WRG01004)));
            }

            var user = await _userManager.FindByNameAsync(model.UserName);

            user.LastLoginDateTime = DateTime.UtcNow;
            await _userManager.UpdateAsync(user);

            UserAuthenticateVM returnVM = new UserAuthenticateVM();

            returnVM       = _mapper.Map <User, UserAuthenticateVM>(user);
            returnVM.Token = GetToken(user);

            return(new JsonResult(returnVM));
        }