public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model,
                                                                  string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var id = await HttpContext.GetExternalIdentity();
                if (id == null)
                {
                    return View("ExternalLoginFailure");
                }
                try
                {
                    // Create a local user and sign in
                    var user = new User(model.UserName);
                    if (await Users.Create(user)
                        && await Logins.Add(new UserLogin(user.Id,
                                                          model.LoginProvider,
                                                          id.FindFirstValue(ClaimTypes.NameIdentifier))))
                    {
                        await SignIn(user.Id,
                                     id.Claims,
                                     isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        return View("ExternalLoginFailure");
                    }
                }
                catch (DbEntityValidationException e)
                {
                    ModelState.AddModelError("",
                                             e.EntityValidationErrors.First()
                                              .ValidationErrors.First()
                                              .ErrorMessage);
                }
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // Create a profile, password, and link the local login before signing in the user
                    var user = new User(model.UserName);
                    if (await Users.Create(user)
                        && await Secrets.Create(new UserSecret(model.UserName,
                                                               model.Password))
                        && await Logins.Add(new UserLogin(user.Id,
                                                          IdentityConfig.LocalLoginProvider,
                                                          model.UserName)))
                    {
                        await SignIn(user.Id,
                                     isPersistent: false);
                        return RedirectToAction("Index",
                                                "Home");
                    }
                    else
                    {
                        ModelState.AddModelError(String.Empty,
                                                 "Failed to create login for: " + model.UserName);
                    }
                }
                catch (DbEntityValidationException e)
                {
                    ModelState.AddModelError("",
                                             e.EntityValidationErrors.First()
                                              .ValidationErrors.First()
                                              .ErrorMessage);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public async Task<ActionResult> ExternalLoginCallback(string loginProvider,
                                                              string returnUrl)
        {
            // Get the information about the user from the external login provider
            var id = await HttpContext.GetExternalIdentity();
            if (id == null)
            {
                return View("ExternalLoginFailure");
            }

            // Make sure the external identity is from the loginProvider we expect
            var providerKeyClaim = id.FindFirst(ClaimTypes.NameIdentifier);
            if (providerKeyClaim == null
                || providerKeyClaim.Issuer != loginProvider)
            {
                return View("ExternalLoginFailure");
            }

            // Succeeded so we should be able to lookup the local user name and sign them in
            var providerKey = providerKeyClaim.Value;
            var userId = await Logins.GetUserId(loginProvider,
                                                providerKey);
            if (!String.IsNullOrEmpty(userId))
            {
                await SignIn(userId,
                             id.Claims,
                             isPersistent: false);
            }
            else
            {
                // No local user for this account
                if (User.Identity.IsAuthenticated)
                {
                    // If the current user is logged in, just add the new account
                    await Logins.Add(new UserLogin(User.Identity.GetUserId(),
                                                   loginProvider,
                                                   providerKey));
                }
                else
                {
                    var user = new User(id.Name);
                    if (await Users.Create(user)
                        && await Logins.Add(new UserLogin(user.Id,
                                                          loginProvider,
                                                          id.FindFirstValue(ClaimTypes.NameIdentifier))))
                    {
                        await SignIn(user.Id,
                                     id.Claims,
                                     isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }

                    //ViewBag.ReturnUrl = returnUrl;
                    //return View("ExternalLoginConfirmation",
                    //            new ExternalLoginConfirmationViewModel
                    //            {
                    //                UserName = id.Name,
                    //                LoginProvider = loginProvider
                    //            });
                }
            }

            return RedirectToLocal(returnUrl);
        }