async Task <bool> PopulateGoogleAuthentication(string TilerUserID, string AccessToken, string RefreshToken, string GoogleEmail, DateTimeOffset ExpirationDate) { //ApplicationDbContext db = new ApplicationDbContext(); TilerUser AppUser = UserManager.FindById(TilerUserID); bool RetValue = false; try { ThirdPartyCalendarAuthenticationModel NewAccountCalendarImportation = new ThirdPartyCalendarAuthenticationModel(); string AuthenticationID = Guid.NewGuid().ToString(); NewAccountCalendarImportation.TilerID = TilerUserID; NewAccountCalendarImportation.ID = AuthenticationID; NewAccountCalendarImportation.Token = AccessToken; NewAccountCalendarImportation.RefreshToken = RefreshToken; NewAccountCalendarImportation.isLongLived = false; NewAccountCalendarImportation.Email = GoogleEmail; NewAccountCalendarImportation.Deadline = ExpirationDate; NewAccountCalendarImportation.ProviderID = ThirdPartyControl.CalendarTool.google.ToString(); //await NewAccountCalendarImportation.refreshAuthenticationToken().ConfigureAwait(false); HttpContext myContext = System.Web.HttpContext.Current; ThirdPartyCalendarAuthenticationModelsController thirdPartyController = new ThirdPartyCalendarAuthenticationModelsController(); await thirdPartyController.CreateGoogle(NewAccountCalendarImportation); RetValue = true; } catch { RetValue = false; } return(RetValue); }
public async Task <ActionResult> deleteGoogleAccount(ThirdPartyAuthenticationForView modelData) { ThirdPartyCalendarAuthenticationModelsController thirdPartyController = new ThirdPartyCalendarAuthenticationModelsController(); await thirdPartyController.deleteGoogleAccount(modelData).ConfigureAwait(false); return(RedirectToAction("ImportCalendar")); }
public async Task <ActionResult> Register(RegisterViewModel model) { ThirdPartyCalendarAuthenticationModelsController thirdPartyController = new ThirdPartyCalendarAuthenticationModelsController(); thirdPartyController.initializeCurrentURI(System.Web.HttpContext.Current.Request.Url.Authority); if (ModelState.IsValid) { int Min = Convert.ToInt32(model.TimeZoneOffSet); TimeSpan OffSet = TimeSpan.FromMinutes(Min); DateTimeOffset EndOfDay = new DateTimeOffset(2014, 1, 1, 22, 0, 0, OffSet); var travelCache = new TravelCache(); var user = new TilerUser { UserName = model.Username, Email = model.Email, FirstName = model.FirstName ?? "", LastName = model.LastName ?? "", EndfOfDay = EndOfDay.UtcDateTime, TravelCache = travelCache }; travelCache.Id = user.Id; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { user = dbContext.Users.Find(user.Id); await createProcrastinateCalendarEvent(user).ConfigureAwait(false); await createAnalysis(user).ConfigureAwait(false); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); Task SendEmail = SendEmailConfirmationAsync(user.Id, "Please Confirm Your Tiler Account!"); await SendEmail.ConfigureAwait(false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); //return RedirectToAction("Index", "Home"); return(RedirectToAction("Desktop", "Account")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
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")); }
public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return(RedirectToAction("Login")); } if (System.Web.HttpContext.Current != null)// this helps save the reurn uri for notification { ThirdPartyCalendarAuthenticationModelsController thirdPartyController = new ThirdPartyCalendarAuthenticationModelsController(); thirdPartyController.initializeCurrentURI(System.Web.HttpContext.Current.Request.Url.Authority); } string ThirdPartyType; var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : false).ConfigureAwait(false); switch (result) { case SignInStatus.Success: { ThirdPartyType = loginInfo.ExternalIdentity.FindFirst("ThirdPartyType").Value; DateTimeOffset Now = DateTimeOffset.UtcNow; if (ThirdPartyType == TilerElements.ThirdPartyControl.CalendarTool.google.ToString()) { string RefreshToken = loginInfo.ExternalIdentity.FindFirst("RefreshToken").Value; if (!string.IsNullOrEmpty(RefreshToken)) { TilerUser AppUser = await UserManager.FindAsync(loginInfo.Login).ConfigureAwait(false); string Email = loginInfo.Email; string AccessToken = loginInfo.ExternalIdentity.FindFirst("AccessToken").Value; TimeSpan fiveMin = new TimeSpan(0, -5, 0); TimeSpan Duration = TimeSpan.Parse(loginInfo.ExternalIdentity.FindFirst("ExpiryDuration").Value); Duration = Duration.Add(fiveMin); Now = Now.Add(Duration); await PopulateGoogleAuthentication(AppUser.Id, AccessToken, RefreshToken, Email, Now).ConfigureAwait(false); } } return(RedirectToAction("Desktop", "Account")); } case SignInStatus.LockedOut: return(View("Lockout")); case SignInStatus.RequiresVerification: return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false })); case SignInStatus.Failure: default: // If the user does not have an account, then prompt the user to create an account ThirdPartyType = loginInfo.ExternalIdentity.FindFirst("ThirdPartyType").Value; ThirdPartyCalendarAuthenticationModel thirdPartyModel = null; DateTimeOffset Deadline = DateTimeOffset.UtcNow; if (ThirdPartyType == TilerElements.ThirdPartyControl.CalendarTool.google.ToString()) { string RefreshToken = loginInfo.ExternalIdentity.FindFirst("RefreshToken").Value; if (!string.IsNullOrEmpty(RefreshToken)) { TilerUser AppUser = await UserManager.FindAsync(loginInfo.Login).ConfigureAwait(false); string Email = loginInfo.Email; string AccessToken = loginInfo.ExternalIdentity.FindFirst("AccessToken").Value; TimeSpan fiveMin = new TimeSpan(0, -5, 0); TimeSpan Duration = TimeSpan.Parse(loginInfo.ExternalIdentity.FindFirst("ExpiryDuration").Value); Duration = Duration.Add(fiveMin); Deadline = Deadline.Add(Duration); thirdPartyModel = new ThirdPartyCalendarAuthenticationModel(); thirdPartyModel.Deadline = Deadline; thirdPartyModel.Email = Email; thirdPartyModel.Token = AccessToken; thirdPartyModel.RefreshToken = RefreshToken; } else { string Email = loginInfo.Email; string AccessToken = loginInfo.ExternalIdentity.FindFirst("AccessToken").Value; thirdPartyModel = new ThirdPartyCalendarAuthenticationModel(); thirdPartyModel.Deadline = Deadline; thirdPartyModel.Email = Email; thirdPartyModel.Token = AccessToken; thirdPartyModel.RefreshToken = RefreshToken; thirdPartyModel.ID = Email; await thirdPartyModel.revokeAccess().ConfigureAwait(false); } } ViewBag.ReturnUrl = returnUrl; ViewBag.LoginProvider = loginInfo.Login.LoginProvider; if (string.IsNullOrEmpty(loginInfo.Email)) { return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email })); } else { return(await ExternalLoginConfirmation(new ExternalLoginConfirmationViewModel { Email = loginInfo.Email }, returnUrl, thirdPartyModel)); } //return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email }); } }
public async Task <ActionResult> RegisterUser(RegisterViewModel model) { ThirdPartyCalendarAuthenticationModelsController thirdPartyController = new ThirdPartyCalendarAuthenticationModelsController(); thirdPartyController.initializeCurrentURI(System.Web.HttpContext.Current.Request.Url.Authority); PostBackData retPost = new PostBackData("Failed to register user", 3); JsonResult RetValue = new JsonResult(); if (ModelState.IsValid) { int Min = Convert.ToInt32(model.TimeZoneOffSet); TimeSpan OffSet = TimeSpan.FromMinutes(Min); DateTimeOffset EndOfDay = new DateTimeOffset(2014, 1, 1, 22, 0, 0, OffSet); var travelCache = new TravelCache(); var user = new TilerUser { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, EndfOfDay = EndOfDay.UtcDateTime, TravelCache = travelCache }; travelCache.Id = user.Id; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { user = dbContext.Users.Find(user.Id); await createProcrastinateCalendarEvent(user).ConfigureAwait(false); await createAnalysis(user).ConfigureAwait(false); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); Task SendEmail = SendEmailConfirmationAsync(user.Id, "Please Confirm Your Tiler Account!"); await SendEmail.ConfigureAwait(false); string LoopBackUrl = ""; if (Request.Browser.IsMobileDevice) { LoopBackUrl = "/Account/Mobile"; } else { LoopBackUrl = "/Account/Desktop"; } retPost = new PostBackData(LoopBackUrl, 0); RetValue.Data = (retPost.getPostBack); return(RetValue); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); } retPost = new PostBackData(string.Join("<br>", result.Errors.Select(obj => obj.Split('.')).SelectMany(obj => obj)), 3); RetValue.Data = (retPost.getPostBack); return(RetValue); } string AllErrors = string.Join("<br>", ModelState.Values.SelectMany(obj => obj.Errors.Select(obj1 => obj1.ErrorMessage))); retPost = new PostBackData(AllErrors, 3); RetValue.Data = (retPost.getPostBack); return(RetValue); }