title: Office 365 API MVC Authentification
body: <p>I am using the MVC Office 365 API libraries and I would like to archieve the following thing: Logging into User-Accounts where I know the username / password and then get there calendar entries. </p>
  
  <p>What I have so far is code that makes this redirect and ask the user to enter credentials. But how can I log in for them wihtout asking? The idea is to get the calendar entries for every user (lets say 20 of them) automatically every few minutes.</p>
  
  <pre><code>public static async Task&lt;IEvent[]&gt; GetCalendarEvents()
  {
      var client = await EnsureClientCreated();
  
      // Obtain calendar event data
      var eventsResults = await (from i in client.Me.Events
                                  where i.End &gt;= DateTimeOffset.UtcNow
                                  select i).Take(10).ExecuteAsync();
  
      var events = eventsResults.CurrentPage.OrderBy(e =&gt; e.Start).ToArray();
  
      return events;
  }
  
  public static async Task&lt;ExchangeClient&gt; EnsureClientCreated()
  {
      var _discoveryContext = await CacheHelper.GetDiscoveryContext();
      var dcr = await _discoveryContext.DiscoverResourceAsync(ServiceResourceId);
      return new ExchangeClient(ServiceEndpointUri, async () =&gt;
      {
          return (await _discoveryContext.AuthenticationContext.AcquireTokenByRefreshTokenAsync(new SessionCache().Read("RefreshToken"),
              new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(_discoveryContext.AppIdentity.ClientId, _discoveryContext.AppIdentity.ClientSecret),
              ServiceResourceId))
              .AccessToken;
      });
  }
  </code></pre>