public async Task<IHttpActionResult> SetCompanyId(string userid, string newclientId)
        {
            var processingResult = new ServiceProcessingResult();
            if (LoggedInUser.Role.IsSuperAdmin()) //make sure user is SA
            {
                //Set the new companyid in the database.
                var dataService = new ApplicationUserDataService();
                processingResult = await dataService.UpdateClientId(newclientId, userid);
                if (processingResult.IsFatalFailure())
                {
                    Logger.Fatal("A fatal error occurred while setting CompanyId");
                    processingResult.IsSuccessful = false;
                }
                else
                {
                    var getLoggedInUserResult = await dataService.GetAsync(LoggedInUserId);
                    if (!getLoggedInUserResult.IsSuccessful)
                    {
                        processingResult.IsSuccessful = false;
                        processingResult.Error = ErrorValues.GENERIC_COULD_NOT_FIND_USER_ERROR;
                        return Ok(processingResult);
                    }
                    var user = getLoggedInUserResult.Data;
                    LoggedInUser.ClientID = newclientId;
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    //Sign Off and back in to refresh cookie.
                    try
                    {
                        await AuthAndUserManager.RefreshAuthenticationCookie(user);
                        processingResult.IsSuccessful = true;
                    }
                    catch (Exception e)
                    {
                        Logger.Fatal("A fatal error occurred while setting CompanyId");
                        processingResult.IsSuccessful = false;
                    }
                }

            }
            return Ok(processingResult);
        }
        public async Task<IHttpActionResult> GetLoggedInUser()
        {
            var processingResult = new ServiceProcessingResult<LoggedInUserViewBindingModel>{ IsSuccessful = true };

            var loggedInUserId = LoggedInUserId;

            var appUserService = new ApplicationUserDataService();
            var loggedInUser = await appUserService.GetAsync(loggedInUserId);
            if (!loggedInUser.IsSuccessful)
            {
                processingResult.IsSuccessful = false;
                processingResult.Error = ErrorValues.GET_LOGGED_IN_USER_INFO_ERROR;
                return Ok(processingResult);
            }

            var user = loggedInUser.Data;
            processingResult.Data = user.ToLoggedInUserViewBindingModel();

            return Ok(processingResult);
        }
        public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByEmailAsync(model.Email);
                if (user == null)
                {
                    ModelState.AddModelError("", "The user either does not exist or is not confirmed.");
                    return View();
                }
                if (user.Role == null)
                {
                    var appUserService = new ApplicationUserDataService();
                    var getUserResult = await appUserService.GetAsync(user.Id);
                    if (!getUserResult.IsSuccessful)
                    {
                        ModelState.AddModelError("",
                            "An error occurred while trying to verify you have permissions to perform this action. Please try again.");
                        return View();
                    }

                    user.Role = getUserResult.Data.Role;
                }
                if (!user.Role.IsAdmin())
                {
                    ModelState.AddModelError("",
                        "Your account cannot perform this action. If you need your password reset, please contact your Manager or and Administrator.");
                    return View();
                }

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Authentication", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");
                return RedirectToAction("ForgotPasswordConfirmation", "Authentication");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }