title: Executing code after user has been authenticated using Azure Active Directory
body: <p>I created an ASP.NET MVC Core (1.1.0) application using VS2015. In the dialog, I selected the option to connect to Azure AD, so VS generated the boilerplate code and, as expected, the app redirects me to Microsoft's login page, where I can login with my work&amp;school account. </p>
  
  <p>Now, after the user logs in, and before serving the first page (say, /home/index) I need to get some information from the user that I have stored in a database (like the display name, the contact information such as an email, phone number, address, a picture of the user, and so on). </p>
  
  <p>What I have thought so far is to add a ControllerBase with a method that retrieves this info, and then pass it to the views via ViewData. But querying the database for this info over and over seems inefficient. An alternative would be to store this info in a cookie or in a session state, thus only hitting the database once. But having to depend on a ControllerBase could lead to errors (for instance, if in some controller method one forgets to call the base's method) and doesn't feel like they way to go. Also, having this funcionality on the home controller only could fail if a user enters the URL with a predefined path (as in www.myapp.com/Users/joedoe/Detail). </p>
  
  <p>I searched and found a reference to using the Events property in the OpenIdConnectOptions object passed to the application builder in the Startup class: </p>
  
  <pre><code>app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
  {
      ClientId = Configuration["Authentication:AzureAd:ClientId"],
      Authority = Configuration["Authentication:AzureAd:AADInstance"] + Configuration["Authentication:AzureAd:TenantId"],
      CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
      Events = new OpenIdConnectEvents {
          //
      }
  });
  </code></pre>
  
  <p>However, the OpenIdConnectEvents class does not have some "OnAuthenticationSucceeded" event, it only has an OnAuthenticationFailed, which is not what I want, and other callbacks whose names doesn't seem to be what I'm looking for. </p>
  
  <p>So, my question, what is the callback I should be using with OpenIdConnectEvents, or, alternatively, what's the preferred way for ASP.NET MVC Core applications that connect to AAD to catch an event after the user has been authenticated?</p>
  
  <p>Thanks in advance. </p>