Esempio n. 1
0
        public async Task <ActionResult> CreateGoogle([Bind(Include = "ID,isLongLived,Email,Token,RefreshToken,ProviderID,Deadline")] ThirdPartyCalendarAuthenticationModel thirdPartyCalendarAuthentication, bool deletGoogleCalendarFromLog = false)
        {
            if (ModelState.IsValid)
            {
                bool makeAdditionCall = false;
                if (!deletGoogleCalendarFromLog)
                {
                    ThirdPartyCalendarAuthenticationModelsController thirdPartyController = new ThirdPartyCalendarAuthenticationModelsController();
                    if (System.Web.HttpContext.Current != null)// this helps save the reurn uri for notification
                    {
                        thirdPartyController.initializeCurrentURI(System.Web.HttpContext.Current.Request.Url.Authority);
                    }
                    bool successInCreation = await thirdPartyController.CreateGoogle(thirdPartyCalendarAuthentication).ConfigureAwait(false);

                    if (successInCreation)
                    {
                        ;
                    }
                    else
                    {
                        ;
                    }
                    return(RedirectToAction("ImportCalendar"));
                }
                else
                {
                    Object[] Param = { thirdPartyCalendarAuthentication.TilerID, thirdPartyCalendarAuthentication.Email, thirdPartyCalendarAuthentication.ProviderID };
                    ThirdPartyCalendarAuthenticationModel checkingThirdPartyCalendarAuthentication = await dbContext.ThirdPartyAuthentication.FindAsync(Param);

                    if (checkingThirdPartyCalendarAuthentication != null)
                    {
                        await deleteGoogleAccount(thirdPartyCalendarAuthentication.getThirdPartyOut()).ConfigureAwait(false);
                    }
                }
                return(RedirectToAction("ImportCalendar"));
            }

            return(View("ImportCalendar"));
        }
Esempio n. 2
0
        async public Task <ActionResult> ImportGoogle(CancellationToken cancellationToken)
        {
            string UserID = User.Identity.GetUserId();
            //var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata(UserID)).
            var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken);

            Google.Apis.Auth.OAuth2.UserCredential myCredential = result.Credential;



            if (myCredential != null)
            {
                var service = new Oauth2Service(new BaseClientService.Initializer
                {
                    HttpClientInitializer = myCredential,
                    ApplicationName       = "ASP.NET MVC Sample"
                });

                bool ResetThirdparty = false;

                ThirdPartyCalendarAuthenticationModel thirdpartydata = new ThirdPartyCalendarAuthenticationModel();
                Google.Apis.Oauth2.v2.Data.Userinfo   userInfo;
                try
                {
                    userInfo = service.Userinfo.Get().Execute();
                }
                catch (Exception e)
                {
                    ResetThirdparty = true;
                    return(RedirectToAction("ImportGoogle"));
                }



                string Email = userInfo.Email;

                if (myCredential.Token.RefreshToken != null)  //if user hasn't authenticated tiler before
                {
                    thirdpartydata.Email       = Email;
                    thirdpartydata.TilerID     = UserID;
                    thirdpartydata.ID          = myCredential.UserId;
                    thirdpartydata.isLongLived = false;
                    double   totalSeconds = myCredential.Token.ExpiresInSeconds == null ? 0 : (double)myCredential.Token.ExpiresInSeconds;
                    DateTime myDate       = myCredential.Token.Issued.AddSeconds(totalSeconds);
                    thirdpartydata.Deadline     = new DateTimeOffset(myDate);
                    thirdpartydata.ProviderID   = TilerElements.ThirdPartyControl.CalendarTool.google.ToString();
                    thirdpartydata.Token        = myCredential.Token.AccessToken;
                    thirdpartydata.RefreshToken = myCredential.Token.RefreshToken;

                    return(await CreateGoogle(thirdpartydata).ConfigureAwait(false));
                }
                else     //if user hasn authenticated tiler before, then update current credentials
                {
                    ThirdPartyCalendarAuthenticationModel retrievedAuthentication = await getGoogleAuthenticationData(UserID, Email).ConfigureAwait(false);

                    try
                    {
                        await retrievedAuthentication.refreshAndCommitToken(dbContext).ConfigureAwait(false);
                    }
                    catch
                    {
                        if (retrievedAuthentication != null)
                        {
                            deleteGoogleAccount(retrievedAuthentication.getThirdPartyOut()).RunSynchronously();
                        }
                        else if (myCredential.Token != null)
                        {
                            thirdpartydata.Email       = Email;
                            thirdpartydata.TilerID     = UserID;
                            thirdpartydata.ID          = myCredential.UserId;
                            thirdpartydata.isLongLived = false;
                            double   totalSeconds = myCredential.Token.ExpiresInSeconds == null ? 0 : (double)myCredential.Token.ExpiresInSeconds;
                            DateTime myDate       = myCredential.Token.IssuedUtc.AddSeconds(totalSeconds);
                            thirdpartydata.Deadline     = new DateTimeOffset(myDate);
                            thirdpartydata.ProviderID   = TilerElements.ThirdPartyControl.CalendarTool.google.ToString();
                            thirdpartydata.Token        = myCredential.Token.AccessToken;
                            thirdpartydata.RefreshToken = myCredential.Token.RefreshToken;
                            await thirdpartydata.unCommitAuthentication().ConfigureAwait(false);

                            return(await ImportGoogle(cancellationToken).ConfigureAwait(false));
                        }
                    }

                    return(RedirectToAction("ImportCalendar"));
                }
            }
            else
            {
                //return View();
                return(new RedirectResult(result.RedirectUri));
            }
        }