public ActionResult GoogleAuthorization(string code)
        {
            // Retrieve the authenticator and save it in session for future use
            var authenticator = GoogleAutherizationHelper.GetAuthenticator(code);

            Session["authenticator"] = authenticator;

            // Save the refresh token locally
            using (var dbContext = new CalSiteDbContext())
            {
                var userName     = User.Identity.Name;
                var userRegistry = dbContext.GoogleRefreshTokens.FirstOrDefault(c => c.UserName == userName);

                if (userRegistry == null)
                {
                    dbContext.GoogleRefreshTokens.Add(
                        new GoogleRefreshToken()
                    {
                        UserName     = userName,
                        RefreshToken = authenticator.RefreshToken
                    });
                }
                else
                {
                    userRegistry.RefreshToken = authenticator.RefreshToken;
                }

                dbContext.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }
        private GoogleAuthenticator GetAuthenticator()
        {
            var authenticator = (GoogleAuthenticator)Session["authenticator"];

            if (authenticator == null || !authenticator.IsValid)
            {
                // Get a new Authenticator using the Refresh Token
                var refreshToken = new CalSiteDbContext().GoogleRefreshTokens.FirstOrDefault(c => c.UserName == User.Identity.Name).RefreshToken;
                authenticator            = GoogleAutherizationHelper.RefreshAuthenticator(refreshToken);
                Session["authenticator"] = authenticator;
            }

            return(authenticator);
        }
 public ActionResult Register(RegisterModel model)
 {
     if (ModelState.IsValid)
     {
         try
         {
             WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email });
             WebSecurity.Login(model.UserName, model.Password);
             string url = GoogleAutherizationHelper.GetAuthorizationUrl(model.Email);
             Response.Redirect(url);
             return(RedirectToAction("Index", "Home"));
         }
         catch (Exception)
         {
             throw;
         }
     }
     return(View());
 }