public async Task <JsonResult> Register([FromBody] UserRegisterVM model) { if (!ModelState.IsValid) { return(new JsonAPIResult(_apiResult.CreateVMWithModelState(modelStateDictionary: ModelState), StatusCodes.Status400BadRequest)); } User entity = _mapper.Map <UserRegisterVM, User>(model); entity.Id = Guid.NewGuid(); entity.CreateDateTime = DateTime.UtcNow; var claims = new Claim[] { new Claim(JwtRegisteredClaimNames.Sub, entity.Id.ToString()), new Claim(JwtRegisteredClaimNames.UniqueName, entity.UserName), new Claim("UserId", entity.Id.ToString()) }; TokenCacheVM authData = new TokenCacheVM(); authData.AccessToken = _tokenService.GenerateAccessToken(claims); authData.RefreshToken = _tokenService.GenerateRefreshToken(); authData.AccessTokenExpiryTime = new JwtSecurityTokenHandler().ReadToken(authData.AccessToken)?.ValidTo ?? DateTime.MinValue; authData.RefreshTokenExpiryTime = DateTime.UtcNow.AddDays(1); authData.Id = entity.Id; var identityResult = await _userManager.CreateAsync(entity, model.Password); if (!identityResult.Succeeded) { return(new JsonAPIResult(_apiResult.CreateVMWithIdentityErrors(errors: identityResult.Errors), StatusCodes.Status422UnprocessableEntity)); } TimeSpan expiryTimeSpan = TimeSpan.FromSeconds(_tokenService.GetTokenExpiryDuration()); var cacheResult = _tokenService.Redis.Insert(authData, expiryTimeSpan); if (!cacheResult.IsSuccessful) { return(new JsonAPIResult(_apiResult.CreateVMWithStatusCode(null, false, APIStatusCode.ERR01011), StatusCodes.Status409Conflict)); } await _signInManager.SignInAsync(entity, isPersistent : false); UserAuthenticationVM returnVM = new UserAuthenticationVM(); returnVM = _mapper.Map <User, UserAuthenticationVM>(entity); returnVM.TokenData = authData; return(new JsonAPIResult(_apiResult.CreateVMWithRec <UserAuthenticationVM>(returnVM, entity.Id, true), StatusCodes.Status201Created)); }