public async Task <HttpResponseMessage> ExternalLoginAsync(SocialLoginDTO userData)
        {
            var userStore    = new UserStore <ApplicationUser>(new ApplicationDbContext());
            var manager      = new UserManager <ApplicationUser>(userStore);
            var existingUser = manager.FindByEmail(userData.Email);

            if (existingUser == null)
            {
                var user = new ApplicationUser();
                user.UserName       = (userData.Email).Split('@')[0];
                user.FirstName      = userData.FirstName;
                user.LastName       = userData.LastName;
                user.Email          = userData.Email;
                user.EmailConfirmed = true;
                user.CityID         = userData.CityId;
                IdentityResult result = manager.Create(user);
                if (result.Succeeded)
                {
                    manager.AddToRole(user.Id, "NormalUser");
                    var userLoginInfo = new UserLoginInfo(userData.Provider, userData.Id);
                    manager.AddLogin(user.Id, userLoginInfo);
                    List <string> rolename = manager.GetRoles(user.Id).ToList();
                    return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.OK, rolename));
                }
                return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.BadRequest, "Failed"));
            }
            else
            {
                var userLogIn = manager.Find(new UserLoginInfo(userData.Provider, userData.Id));
                if (userLogIn != null)
                {
                    List <string> rolename = manager.GetRoles(userLogIn.Id).ToList();
                    return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.OK, rolename));
                }
                else
                {
                    return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.BadRequest, "login Failed"));
                }
            }
        }
        public async Task <HttpResponseMessage> ChangePasswordAsync(AccountModel model)
        {
            try
            {
                string          emailId    = model.Email;
                string          password   = model.Password;
                ApplicationUser RecoveryId = db.Users.SingleOrDefault(x => x.Email == emailId);

                var userStore = new UserStore <ApplicationUser>(new ApplicationDbContext());
                var manager   = new UserManager <ApplicationUser>(userStore);

                var currentUser     = manager.FindByEmail(emailId);
                var newPasswordHash = manager.PasswordHasher.HashPassword(password);
                await userStore.SetPasswordHashAsync(currentUser, newPasswordHash);

                await manager.UpdateAsync(currentUser);

                return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.Accepted, "New Password Updated"));
            }
            catch (Exception ex)
            {
                return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.BadRequest, "Failed to update new Password"));
            }
        }
        public async Task <HttpResponseMessage> RegisterAsync(AccountModel model)
        {
            try
            {
                if (model == null)
                {
                    return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.BadRequest, "Model is null, please check request"));
                }

                if (!ModelState.IsValid)
                {
                    string modelErrorMessage = string.Join(" | ", ModelState.Values
                                                           .SelectMany(v => v.Errors)
                                                           .Select(e => e.ErrorMessage));

                    return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.BadRequest, modelErrorMessage));
                }


                var userStore = new UserStore <ApplicationUser>(new ApplicationDbContext());
                var manager   = new UserManager <ApplicationUser>(userStore);
                var user      = new ApplicationUser
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    PhoneNumber = model.PhoneNumber,
                    CityID      = model.CityID
                };

                manager.PasswordValidator = new PasswordValidator
                {
                    RequiredLength = 3
                };

                IdentityResult result = manager.Create(user, model.Password);

                if (result.Succeeded)
                {
                    manager.AddToRole(user.Id, "NormalUser");

                    ////send confirmation email

                    //var provider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("PlanYourTrip");
                    //manager.UserTokenProvider = new Microsoft.AspNet.Identity.Owin.DataProtectorTokenProvider<ApplicationUser>(provider.Create("EmailConfirmation"));

                    //var code = await manager.GenerateEmailConfirmationTokenAsync(user.Id);

                    //var urlLink = Url.Link("Default", new { controller="Account",action="verify-email",id = user.Id, token = code });

                    //StringBuilder body = new StringBuilder();
                    //body.Append(@"<a href='" + urlLink + "'>Click Here To Verify your Account</a>");
                    //IdentityMessage myMessage = new IdentityMessage()
                    //{
                    //    Body = body.ToString(),
                    //    Destination = user.Email,
                    //    Subject = "Confirm your mail"
                    //};
                    //EmailService sendEmail = new EmailService();
                    //await sendEmail.SendAsync(myMessage);

                    return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.Created, "User added"));
                }

                string identityErrorMessage = string.Join("\n", result.Errors
                                                          .Select(x => x));
                return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.BadRequest, identityErrorMessage));
            }
            catch (Exception ex)
            {
                return(HTTPBusinessLogic.SetHttpResponse(HttpStatusCode.InternalServerError, "Failed because: {ex.Message}"));
            }
        }