public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
              {
            try
            {
              using (DataContext db = new DataContext())
              {
            UserProfile user = new UserProfile() { UserName = model.UserName, Password = model.Password };
            db.UserProfiles.Add(user);
            db.SaveChanges();
              }
              FormsAuthentication.SetAuthCookie(model.UserName, false);
              return RedirectToAction("Index", "Home");
            }
            catch (MembershipCreateUserException e)
            {
              ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
              }

              // If we got this far, something failed, redisplay form
              return View(model);
        }
        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);
        }