//[HttpPost]
        //public async Task<IActionResult> Facebook([FromBody] UserTokenIdModel token)
        //{
        //    try
        //    {
        //        var user = await _facebookAuthService.Authenticate(token);
        //        await _signInManager.SignInAsync(user, true);
        //        //var jwtToken = GenerateJwtToken(user.Email, user);
        //        // authentication successful, then generate jwt token
        //        string tokenInString = Elmah.MvcCore.Security.CustomizedClaimTypes.GetJwtSecurityTokenInString(user.Id.ToLower(), _appSettings.Secret);
        //        return Ok(tokenInString);
        //    }
        //    catch (Exception e)
        //    {
        //        Debug.WriteLine(e.Message);
        //        return BadRequest(e.Message);
        //    }
        //}

        private async Task <Framework.WebApi.AuthenticationResponse> GetAuthenticationResponse(
            Elmah.MvcCore.Models.ApplicationUser user)
        {
            var loginResponse = new Framework.WebApi.AuthenticationResponse
            {
                Succeeded = true
                ,
                IsLockedOut = false
                ,
                IsNotAllowed = false
                ,
                RequiresTwoFactor = false
                ,
                EntityID = user != null ? user.EntityID : null
            };

            // authentication successful, then generate jwt token
            string tokenInString = Elmah.MvcCore.Security.CustomizedClaimTypes.GetJwtSecurityTokenInString(user.Id.ToLower(), _appSettings.Secret);

            loginResponse.Token = tokenInString;

            // Load LogIn User related data
            if (loginResponse.Succeeded)
            {
                loginResponse.Roles = await _userManager.GetRolesAsync(user);
            }

            return(loginResponse);
        }
        public async Task <Framework.WebApi.AuthenticationResponse> Register([FromBody] Elmah.MvcCore.Models.AccountViewModels.RegisterViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(new Framework.WebApi.AuthenticationResponse {
                    Succeeded = false
                });
            }

            var user = new Elmah.MvcCore.Models.ApplicationUser()
            {
                UserName = model.Email, Email = model.Email
            };

            var result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(new Framework.WebApi.AuthenticationResponse {
                    Succeeded = false
                });
            }
            else
            {
                // This is a copy from Register method in AccountController.
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                /*
                 * var service1 = _serviceProvider.GetRequiredService<NTierOnTime.WcfContracts.IEntityService>();
                 *
                 * var response = await NTierOnTime.CoreCommonBLL.Helpers.EntityHelper.CreateNewEntity(service1, model.Email, _logger);
                 *
                 * if (response.BusinessLogicLayerResponseStatus == Framework.Services.BusinessLogicLayerResponseStatus.MessageOK || response.BusinessLogicLayerResponseStatus == Framework.Services.BusinessLogicLayerResponseStatus.UIProcessReady)
                 * {
                 *  var applicationUser = await _userManager.FindByEmailAsync(model.Email);
                 *  if (applicationUser != null)
                 *  {
                 *      applicationUser.EntityID = response.Message[0].EntityID;
                 *      await _userManager.UpdateAsync(applicationUser);
                 *  }
                 * }
                 */
            }

            var loginViewModel = new Elmah.MvcCore.Models.AccountViewModels.LoginViewModel {
                Email = model.Email, Password = model.Password
            };

            return(await Login(loginViewModel));
        }
        private async Task <Framework.WebApi.AuthenticationResponse> GetAuthenticationResponse(
            Elmah.MvcCore.Models.ApplicationUser user
            , Microsoft.AspNetCore.Identity.SignInResult result)
        {
            var loginResponse = new Framework.WebApi.AuthenticationResponse
            {
                Succeeded = result.Succeeded
                ,
                IsLockedOut = result.IsLockedOut
                ,
                IsNotAllowed = result.IsNotAllowed
                ,
                RequiresTwoFactor = result.RequiresTwoFactor
                ,
                EntityID = user != null ? user.EntityID : null
            };

            // authentication successful, then generate jwt token
            string tokenInString = Elmah.MvcCore.Security.CustomizedClaimTypes.GetJwtSecurityTokenInString(user.Id.ToLower(), _appSettings.Secret);

            loginResponse.Token = tokenInString;

            // Load LogIn User related data
            if (loginResponse.Succeeded)
            {
                loginResponse.Roles = await _userManager.GetRolesAsync(user);

                #region TODO: Load more data to LoginResponse

                //// TODO: Load more data to LoginResponse
                //using (var scope = _serviceProvider.CreateScope())
                //{
                //    var criteria = new NTierOnTime.CommonBLLEntities.EntityChainedQueryCriteriaIdentifier();
                //    criteria.Identifier.EntityID.NullableValueToCompare = user.EntityID;

                //    var entityResponse = new NTierOnTime.AspNetMvcCoreViewModel.Entity.DashboardVM(); // TODO: how to IoC
                //    entityResponse.CriteriaOfMasterEntity = criteria;
                //    entityResponse.SetServiceProvider(this._serviceProvider);
                //    await entityResponse.LoadData(
                //        isToLoadFK_CourseCategory_Entity_ParentEntityID_List: false
                //        , isToLoadFK_Album_Entity_Owner_List: false
                //        , isToLoadFK_Comment_Entity_PostedByID_List: false
                //        , isToLoadFK_EntityAddress_Entity_EntityID_List: false
                //        , isToLoadFK_EntityAlbum_Entity_EntityID_List: false
                //        , isToLoadFK_EntityCalendarItem_Entity_EntityID_List: false
                //        , isToLoadFK_EntityCommentThread_Entity_EntityID_List: false
                //        , isToLoadFK_EntityContact_Entity_EntityID_List: false
                //        , isToLoadFK_EntityEmail_Entity_EntityID_List: false
                //        , isToLoadFK_EntityScheduleGroup_Entity_EntityID_List: false
                //        , isToLoadFK_EntityVirtualAddress_Entity_EntityID_List: false
                //        , isToLoadFK_Liking_Entity_EntityID_List: false
                //        , isToLoadFK_Liking_Entity_TheOtherSideEntityID_List: false
                //        , isToLoadFK_MemberProgram_Entity_ProgramEntityID_List: false
                //        , isToLoadFK_Membership_Entity_MasterEntityID_List: false
                //        , isToLoadFK_Membership_Entity_SlaveEntityID_List: true
                //        , isToLoadFK_ProgramScheduleCalendarItem_Entity_ProgramEntityID_List: false
                //        , isToLoadFK_BusinessEntity_Entity_EntityID_FormView: false
                //        , isToLoadFK_Class_Entity_EntityID_FormView: false
                //        , isToLoadFK_Course_Entity_EntityID_FormView: false
                //        , isToLoadFK_ActivitySummary_Entity_EntityID_FormView: false
                //        , isToLoadFK_Membership_Entity_MembershipID_FormView: true
                //        , isToLoadFK_Person_Entity_EntityID_FormView: true);

                //    // 1. Entity
                //    if (entityResponse.StatusOfMasterEntity == Framework.Services.BusinessLogicLayerResponseStatus.MessageOK || entityResponse.StatusOfMasterEntity == Framework.Services.BusinessLogicLayerResponseStatus.UIProcessReady)
                //    {
                //        loginResponse.Entity = entityResponse.MasterEntity;
                //    }

                //    // 2. Person
                //    if (entityResponse.StatusOfFK_Person_Entity_EntityID_FormView == Framework.Services.BusinessLogicLayerResponseStatus.MessageOK || entityResponse.StatusOfFK_Person_Entity_EntityID_FormView == Framework.Services.BusinessLogicLayerResponseStatus.UIProcessReady)
                //    {
                //        loginResponse.HasPerson = entityResponse.StatusOfFK_Person_Entity_EntityID_FormView == Framework.Services.BusinessLogicLayerResponseStatus.MessageOK || entityResponse.StatusOfFK_Person_Entity_EntityID_FormView == Framework.Services.BusinessLogicLayerResponseStatus.UIProcessReady;
                //        loginResponse.Person = entityResponse.FK_Person_Entity_EntityID_FormView;
                //    }

                //    // 3. Joined Memberships
                //    if (entityResponse.StatusOfFK_Membership_Entity_SlaveEntityID_List == Framework.Services.BusinessLogicLayerResponseStatus.MessageOK || entityResponse.StatusOfFK_Membership_Entity_SlaveEntityID_List == Framework.Services.BusinessLogicLayerResponseStatus.UIProcessReady)
                //    {
                //        loginResponse.HasJoinedMemberShip = entityResponse.StatusOfFK_Membership_Entity_SlaveEntityID_List == Framework.Services.BusinessLogicLayerResponseStatus.MessageOK || entityResponse.StatusOfFK_Membership_Entity_SlaveEntityID_List == Framework.Services.BusinessLogicLayerResponseStatus.UIProcessReady;
                //        loginResponse.JoinedMemberships = entityResponse.FK_Membership_Entity_SlaveEntityID_List;
                //    }

                //}

                #endregion TODO: Load more data to LoginResponse
            }
            return(loginResponse);
        }