public ActionResult ExternalLoginCallback(string returnUrl)
        {
            AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
              if (!result.IsSuccessful)
              {
            return RedirectToAction("ExternalLoginFailure");
              }

              using (DataContext db = new DataContext())
              {
            ExternalLoginProfile externalLogin = db.ExternalLoginProfiles.Include("User").FirstOrDefault(p => p.Provider.Equals(result.Provider, StringComparison.OrdinalIgnoreCase) && p.ProviderUserId.Equals(result.ProviderUserId, StringComparison.OrdinalIgnoreCase));
            if (externalLogin != null)
            {
              FormsAuthentication.SetAuthCookie(externalLogin.User.UserName, false);
              return RedirectToLocal(returnUrl);
            }
              }

              using (DataContext db = new DataContext())
              {
            if (User.Identity.IsAuthenticated)
            {
              // If the current user is logged in, add the new account
              ExternalLoginProfile externalLogin = db.ExternalLoginProfiles.FirstOrDefault(p => p.Provider.Equals(result.Provider, StringComparison.OrdinalIgnoreCase) && p.ProviderUserId.Equals(result.ProviderUserId, StringComparison.OrdinalIgnoreCase));
              if (externalLogin == null)
              {
            UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(User.Identity.Name, StringComparison.OrdinalIgnoreCase));
            externalLogin = new ExternalLoginProfile() { User = user, Provider = result.Provider, ProviderUserId = result.ProviderUserId };
            db.ExternalLoginProfiles.Add(externalLogin);
            db.SaveChanges();
              }
              return RedirectToLocal(returnUrl);
            }
            else
            {
              UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(result.UserName, StringComparison.OrdinalIgnoreCase));
              // Check if user already exists
              if (user == null)
              {
            user = new UserProfile { UserName = result.UserName };
            db.UserProfiles.Add(user);
            ExternalLoginProfile externalLogin = new ExternalLoginProfile() { User = user, Provider = result.Provider, ProviderUserId = result.ProviderUserId };
            db.ExternalLoginProfiles.Add(externalLogin);
            db.SaveChanges();

            FormsAuthentication.SetAuthCookie(externalLogin.User.UserName, false);
            return RedirectToLocal(returnUrl);
              }
              else
              {
            // User is new but that username is taken, ask for their desired user name
            string loginData = OAuthWebSecurity.SerializeProviderUserId(result.Provider, result.ProviderUserId);
            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(result.Provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View("ExternalLoginConfirmation", new RegisterExternalLoginModel { UserName = result.UserName, ExternalLoginData = loginData });
              }
            }
              }
        }
        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 (DataContext db = new DataContext())
            {
              UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.Equals(model.UserName, StringComparison.OrdinalIgnoreCase));
              // Check if user already exists
              if (user == null)
              {
            user = new UserProfile { UserName = model.UserName };
            db.UserProfiles.Add(user);
            ExternalLoginProfile externalLogin = new ExternalLoginProfile() { User = user, Provider = provider, ProviderUserId = providerUserId };
            db.ExternalLoginProfiles.Add(externalLogin);
            db.SaveChanges();

            FormsAuthentication.SetAuthCookie(externalLogin.User.UserName, false);
            return RedirectToLocal(returnUrl);
              }
              else
              {
            ModelState.AddModelError("", "User name already exists. Please enter a different user name.");
              }
            }
              }

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