public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
            if (loginInfo == null)
            {
                return RedirectToAction("Login");
            }

            // Sign in the user with this external login provider if the user already has a login
            var user = await UserManager.FindAsync(loginInfo.Login);
            if (user != null)
            {
                await SignInAsync(user, isPersistent: false);

                _profileService.AssociateProfileAuthData(user, loginInfo.ExternalIdentity.Claims.ToList());

                // for now we will grab profile data immediately
                // but we might want to queue this instead
                _profileService.CollectProfileData(user.UserId);
                
                return RedirectToLocal(returnUrl);
            }
            else
            {
                // this user doesn't have a login yet so create one for them
                user = new JobUser() { UserName = loginInfo.Email, Email = loginInfo.Email };
                IdentityResult result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);

                        _profileService.AssociateProfileAuthData(user, loginInfo.ExternalIdentity.Claims.ToList());

                        // for now we will grab profile data immediately
                        // but we might want to queue this instead
                        _profileService.CollectProfileData(user.UserId);

                        return RedirectToLocal(returnUrl);
                    }
                }
            }

            return RedirectToLocal(returnUrl);
        }
 private async Task SignInAsync(JobUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));
 }
 public AuthenticatedApiController()
 {
     _user = null;
     _userStore = new JobUserStore(ConfigurationManager.ConnectionStrings["SurveyDataSource"].ConnectionString);
 }