public async Task<ActionResult> Calendar()
    {
      AuthenticationContext authContext = new AuthenticationContext(authority);

      ClientCredential credential = new ClientCredential(appId, appSecret);
      AuthenticationResult authResult = null;
      try
      {
        authResult = await authContext.AcquireTokenSilentAsync(scopes, credential, UserIdentifier.AnyUser);
      }
      catch (AdalException ex)
      {
        TempData["message"] = "Please sign in to continue";
        return Redirect("/");
      }

      var client = new Outlook();
      client.anchorMailbox = GetUserEmail(authContext, appId);
      ViewBag.UserName = client.anchorMailbox;

      DateTime viewStart = DateTime.Now.ToUniversalTime();
      DateTime viewEnd = viewStart.AddHours(3);
      var result = await client.GetCalendarView(authResult.Token, client.anchorMailbox, viewStart, viewEnd);

      return View(result);
    }
    public async Task<ActionResult> Calendar()
    {
      string userId = (string)Session["user_id"];
      if (string.IsNullOrEmpty(userId))
      {
        TempData["message"] = "Please sign in to continue";
        return Redirect("/");
      }

      OAuthHelper oauthHelper = new OAuthHelper(authority, appId, appSecret);
      string redirectUri = Url.Action("Authorize", "Home", null, Request.Url.Scheme);

      string accessToken = await oauthHelper.GetAccessToken(userId, redirectUri);
      if (string.IsNullOrEmpty(accessToken))
      {
        TempData["message"] = "Please sign in to continue";
        return Redirect("/");
      }

      var client = new Outlook();
      client.anchorMailbox = (string)Session["user_name"];
      ViewBag.UserName = client.anchorMailbox;

      DateTime viewStart = DateTime.Now.ToUniversalTime();
      DateTime viewEnd = viewStart.AddHours(3);
      var result = await client.GetCalendarView(accessToken, client.anchorMailbox, viewStart, viewEnd);

      return View(result);
    }