public ActionResult ExternalLoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));

            if (!result.IsSuccessful)
            {
                return RedirectToAction("ExternalLoginFailure");
            }
            if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: true))
            {
                return RedirectToLocal(returnUrl);
            }
            ExtraData extraData = null;
            if (User.Identity.IsAuthenticated)
            {
                // If the current user is logged in add the new account
                OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);
                return RedirectToLocal(returnUrl);
            }
            else
            {
                // User is new, ask for their desired membership name
                string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
                ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
                ViewBag.ReturnUrl = returnUrl;
                if (result.ExtraData != null)
                {
                    extraData = new ExtraData
                    {
                        Description = result.ExtraData.ContainsKey("description") ? result.ExtraData["description"] : string.Empty,
                        Location = result.ExtraData.ContainsKey("location") ? result.ExtraData["location"] : string.Empty,
                        Name = result.ExtraData.ContainsKey("name") ? result.ExtraData["name"] : String.Empty,
                        AccessToken = result.ExtraData.ContainsKey("accesstoken") ? result.ExtraData["accesstoken"] : string.Empty,
                        Url = result.ExtraData.ContainsKey("profile_image_url") ? result.ExtraData["profile_image_url"] : string.Empty,
                        ParentUserProfile = new UserProfile { UserName = result.UserName }
                    };
                    //using (UsersContext db = new UsersContext())
                    //{
                    //    db.Entry(extraData).State = System.Data.EntityState.Added;
                    //    db.SaveChanges();
                    //}
                }
                return View("ExternalLoginConfirmation", new RegisterExternalLoginModel
                {
                    UserName = result.UserName,
                    ExternalLoginData = loginData,
                    AuthenticationProviderData = extraData
                });
            }
        }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (UsersContext db = new UsersContext())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        ExtraData extraData = new ExtraData
                        {
                            Description = model.AuthenticationProviderData.Description,
                            Location = model.AuthenticationProviderData.Location,
                            Name = model.AuthenticationProviderData.Name,
                            AccessToken = model.AuthenticationProviderData.AccessToken,
                            Url = model.AuthenticationProviderData.Url,
                            ParentUserProfile = new UserProfile { UserName = model.UserName }
                        };
                        // Insert name into the profile table
                        db.Entry(extraData).State = System.Data.EntityState.Added;
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);
                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }