title: AcquireTokenByAuthorizationCode throw new exception in single tenant application using ASP.NET MVC using Azure Active Directory
body: <p>I tried <a href="https://docs.microsoft.com/en-us/learn/modules/msgraph-build-aspnetmvc-apps/5-exercise-add-auth" rel="nofollow noreferrer">this tutorial</a> because I want to use the Microsoft Graph API to create massive teams in Microsoft Teams.
  The only difference with the tutorial is that I used the next choice in Authentication section of Azure AD admin center:</p>
  <pre><code>&quot;Accounts in this organizational directory only (myuniversity only - Single tenant)&quot;
  </code></pre>
  <p>Because of this I changed my code to use endpoint for single tenant</p>
  <pre><code>public void ConfigureAuth(IAppBuilder app)
  {
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  
      app.UseCookieAuthentication(new CookieAuthenticationOptions());
  
      app.UseOpenIdConnectAuthentication(
          new OpenIdConnectAuthenticationOptions
          {
              
              ClientId = appId,
              //Authority = &quot;https://login.microsoftonline.com/common/v2.0&quot;,//
              Authority = &quot;https://login.microsoftonline.com/{tenantid}/v2.0&quot;,
              Scope = $&quot;openid email profile offline_access {graphScopes}&quot;,
              RedirectUri = redirectUri,
              PostLogoutRedirectUri = redirectUri,
              TokenValidationParameters = new TokenValidationParameters
              {
                  // For demo purposes only, see below
                  ValidateIssuer = false
  
                  // In a real multi-tenant app, you would add logic to determine whether the
                  // issuer was from an authorized tenant
                  //ValidateIssuer = true,
                  //IssuerValidator = (issuer, token, tvp) =&gt;
                  //{
                  //  if (MyCustomTenantValidation(issuer))
                  //  {
                  //    return issuer;
                  //  }
                  //  else
                  //  {
                  //    throw new SecurityTokenInvalidIssuerException(&quot;Invalid issuer&quot;);
                  //  }
                  //}
              },
              Notifications = new OpenIdConnectAuthenticationNotifications
              {
                  AuthenticationFailed = OnAuthenticationFailedAsync,
                  AuthorizationCodeReceived = OnAuthorizationCodeReceivedAsync
              }
          }
      );
  }
  </code></pre>
  <p>After authentication of the user the code I run to <code>OnAuthorizationCodeReceivedAsync</code> method but got an exception in <code>AcquireTokenByAuthorizationCode</code> method</p>
  <p>Here is the method</p>
  <pre><code>private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification)
  {
      var idClient = ConfidentialClientApplicationBuilder.Create(appId)
          .WithRedirectUri(redirectUri)
          .WithClientSecret(appSecret)
          .Build();
      var accounts = await idClient.GetAccountsAsync();
      string message;
      string debug;
  
      try
      {
          string[] scopes = graphScopes.Split(' ');
  
          var result = await idClient.AcquireTokenByAuthorizationCode(scopes, notification.Code).ExecuteAsync();
  
          message = &quot;Access token retrieved.&quot;;
          debug = result.AccessToken;
      }
      catch (MsalException ex)
      {
          message = &quot;AcquireTokenByAuthorizationCodeAsync threw an exception&quot;;
          debug = ex.Message;
      }
  
      var queryString = $&quot;message={message}&amp;debug={debug}&quot;;
      if (queryString.Length &gt; 2048)
      {
          queryString = queryString.Substring(0, 2040) + &quot;...&quot;;
      }
  
      notification.HandleResponse();
      notification.Response.Redirect($&quot;/Home/Error?{queryString}&quot;);
  }
  </code></pre>
  <p>The exception is:</p>
  <blockquote>
  <p>AcquireTokenByAuthorizationCodeAsync threw an exception</p>
  <p>AADSTS50194: Application 'application id'(ASP.NET Graph Tutorial) is
  not configured as a multi-tenant application. Usage of the /common
  endpoint is not supported for such applications created after
  '10/15/2018'. Use a tenant-specific endpoint or configure the
  application to be multi-tenant. Trace ID:
  5f0fbf2e-5d63-40d4-a833-ca8627a02d00</p>
  <p>Correlation ID: 3ec4ec7b-0c86-4e2b-a053-9823f977499d Timestamp:
  2021-02-16 20:21:03Z</p>
  </blockquote>
  <p>I want to use single-tenant authentication for my organization only</p>