public async Task<ActionResult> DriveAsync(CancellationToken cancellationToken, bool? createNew) { var result = await new AuthorizationCodeMvcApp(this, new StorageHub.Utility.DriveFlowMetadata()). AuthorizeAsync(cancellationToken); if (result.Credential == null) return new RedirectResult(result.RedirectUri); var driveService = new DriveService(new BaseClientService.Initializer { HttpClientInitializer = result.Credential, ApplicationName = "StorageHub" }); if(createNew != null && createNew == true) { string currentUserId = User.Identity.GetUserId(); var currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId); StorageService driveStorage = new StorageService() { ServiceType = 1 }; currentUser.StorageServices.Add(driveStorage); db.SaveChanges(); } Session["GoogleDriveStatus"] = StorageService.ServiceStatus.Connected; Session["GoogleDriveService"] = driveService; return RedirectToAction("Index", "Home"); }
public ActionResult DropboxCallback(string code, string state) { if (code == null) { ViewBag.Message = "Application not authorized to acces user's files"; return RedirectToAction("Manage", "StorageManagement"); } var t = Session["DropboxStateString"]; if (!state.Equals(Session["DropboxStateString"])) return new HttpUnauthorizedResult(); Session.Remove("DropboxStateString"); var client = new DropNetClient(AppCredentials.DROPBOX_APP_KEY, AppCredentials.DROPBOX_APP_SECRET, DropNetClient.AuthenticationMethod.OAuth2); var callbackUrl = Url.Action("DropboxCallback", "StorageManagement", null, Request.Url.Scheme); var accessToken = client.GetAccessToken(code, callbackUrl); string currentUserId = User.Identity.GetUserId(); var currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId); StorageService dropboxService = new StorageService { ServiceType = StorageService.ServiceTypes.Dropbox, AccessToken = Utility.Encryption.Encrypt(accessToken.Token) }; currentUser.StorageServices.Add(dropboxService); db.SaveChanges(); Session["DropBoxStatus"] = StorageService.ServiceStatus.Connected; Session["DropBoxClient"] = client; return RedirectToAction("Index", "Home"); }