public ActionResult Index() { OneDriveUser user = OneDriveUser.UserForRequest(this.Request); ViewBag.ShowSignInButtons = (user == null); return(View()); }
// Create webhook subscription public async Task <ActionResult> CreateSubscription() { #region Create OneDriveClient for current user OneDriveUser user = OneDriveUser.UserForRequest(this.Request); if (null == user) { return(Redirect(Url.Action("Index", "Home"))); } var client = await GetOneDriveClientAsync(user); #endregion // Ensure the app folder is created first var appFolder = await client.Drive.Special["approot"].Request().GetAsync(); // Create a subscription on the drive var notificationUrl = ConfigurationManager.AppSettings["ida:NotificationUrl"]; Models.OneDriveSubscription subscription = new OneDriveSubscription { NotificationUrl = notificationUrl, ClientState = "my client state" }; FixPPESubscriptionBug(user, subscription); // Because the OneDrive SDK does not support OneDrive subscriptions natively yet, // we use BaseRequest to generate a request the SDK can understand. You could also use HttpClient var request = new BaseRequest(client.BaseUrl + "/drive/root/subscriptions", client) { Method = "POST", ContentType = "application/json" }; try { var subscriptionResponse = await request.SendAsync <Models.OneDriveSubscription>(subscription); if (null != subscriptionResponse) { // Store the subscription ID so we can keep track of which subscriptions are tied to which users user.SubscriptionId = subscriptionResponse.SubscriptionId; Models.SubscriptionViewModel viewModel = new Models.SubscriptionViewModel { Subscription = subscriptionResponse }; return(View("Subscription", viewModel)); } } catch (Exception ex) { ViewBag.Message = ex.Message; } return(View("Error")); }
/// <summary> /// Delete the user's active subscription and then redirect to logout /// </summary> /// <returns></returns> public async Task <ActionResult> DeleteSubscription() { OneDriveUser user = OneDriveUser.UserForRequest(this.Request); if (null == user) { return(Redirect(Url.Action("Index", "Home"))); } if (!string.IsNullOrEmpty(user.SubscriptionId)) { var client = await GetOneDriveClientAsync(user); // Because the OneDrive SDK does not support OneDrive subscriptions natively yet, // we use BaseRequest to generate a request the SDK can understand var request = new BaseRequest(client.BaseUrl + "/drive/root/subscriptions/" + user.SubscriptionId, client) { Method = "DELETE" }; try { var response = await request.SendRequestAsync(null); if (!response.IsSuccessStatusCode) { ViewBag.Message = response.ReasonPhrase; return(View("Error")); } else { user.SubscriptionId = null; } } catch (Exception ex) { ViewBag.Message = ex.Message; return(View("Error")); } } return(RedirectToAction("SignOut", "Account")); }