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 SimpleMembershipInitializer()
            {
                try
                {
                    using (var context = new CalSiteDbContext())
                    {
                        context.Database.CreateIfNotExists();
                        context.Database.Initialize(true);
                    }

                    WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
예제 #4
0
        public ActionResult ListEvents(DateTime startDate, DateTime endDate)
        {
            UserProfile userProfile = null;

            using (var context = new CalSiteDbContext())
            {
                userProfile = context.UserProfiles.FirstOrDefault(c => c.UserName == User.Identity.Name);
            }

            if (userProfile == null)
            {
                return(RedirectToAction("Register", "Account"));
            }

            var authenticator = GetAuthenticator();

            var service = new GoogleCalendarServiceProxy(authenticator);
            var model   = service.GetEvents(userProfile.Email, startDate, endDate);

            ViewBag.StartDate = startDate.ToShortDateString();
            ViewBag.EndDate   = endDate.ToShortDateString();
            return(View("Index", model));
        }
        public ActionResult Create()
        {
            string calendarId = string.Empty;

            using (var context = new CalSiteDbContext())
            {
                calendarId = context.UserProfiles.FirstOrDefault(c => c.UserName == User.Identity.Name).Email;
            }

            var model = new CalendarEvent()
            {
                CalendarId = calendarId,
                StartDate  = DateTime.Today,
                EndDate    = DateTime.Today.AddMinutes(60)
            };
            var colorList = Enum.GetValues(typeof(GoogleEventColors)).Cast <GoogleEventColors>()
                            .Select(v => new SelectListItem {
                Text = v.ToString(), Value = ((int)v).ToString()
            });

            ViewBag.Colors = new SelectList(colorList, "Value", "Text");
            return(View(model));
        }