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 result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : true);

            switch (result)
            {
            case SignInStatus.Success:
            {
                // grab hold of the access token from the strava external login
                Claim stravaTokenClaim        = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:accesstoken");
                Claim stravaRefreshTokenClaim = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:refreshtoken");
                Claim stravaExpiresAtClaim    = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:expiresat");
                Claim stravaExpiresInClaim    = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:expiresin");

                long stravaAthleteId = Convert.ToInt64(loginInfo.Login.ProviderKey);

                // update details from current strava athlete settings.
                FitnessViewer.Infrastructure.Helpers.StravaAthlete s = new Infrastructure.Helpers.StravaAthlete(stravaAthleteId, stravaTokenClaim.Value, stravaRefreshTokenClaim.Value, Convert.ToInt32(stravaExpiresAtClaim.Value), Convert.ToInt32(stravaExpiresInClaim.Value));
                if (!s.UpdateAthlete(stravaTokenClaim.Value))
                {
                    // error occured somewhere during account creation.  ASP.NET user record exists but not an Athlete record
                    var userDetails = await UserManager.FindByEmailAsync(loginInfo.Email);

                    if (userDetails != null)
                    {
                        s.AddAthlete(userDetails.Id, stravaTokenClaim.Value, stravaRefreshTokenClaim.Value, Convert.ToInt32(stravaExpiresAtClaim.Value), Convert.ToInt32(stravaExpiresInClaim.Value));
                    }
                }

                return(RedirectToLocal(returnUrl));
            }

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }));

            case SignInStatus.Failure:
            default:
            {
                ExternalLoginConfirmationViewModel model = new ExternalLoginConfirmationViewModel();
                model.Email = loginInfo.Email;
                return(await ExternalLoginConfirmation(model, null));
            }

                //// If the user does not have an account, then prompt the user to create an account
                //ViewBag.ReturnUrl = returnUrl;
                //ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                //return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
            }
        }
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        // at this point the user exists so grab hold of the access token from the strava external login
                        Claim stravaTokenClaim        = info.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:accesstoken");
                        Claim stravaRefreshTokenClaim = info.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:refreshtoken");
                        Claim stravaExpiresAtClaim    = info.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:expiresat");
                        Claim stravaExpiresInClaim    = info.ExternalIdentity.Claims.First(x => x.Type == "urn:strava:expiresin");

                        // create a new strava athlete record and store the UserId and access token.
                        FitnessViewer.Infrastructure.Helpers.StravaAthlete s = new Infrastructure.Helpers.StravaAthlete();
                        s.AddAthlete(user.Id, stravaTokenClaim.Value, stravaRefreshTokenClaim.Value, Convert.ToInt32(stravaExpiresAtClaim.Value), Convert.ToInt32(stravaExpiresInClaim.Value));

                        await SignInManager.SignInAsync(user, isPersistent : true, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }