title: 'AADSTS70000: Transmission data parser failure: Authorization Code is malformed or invalid
body: <p>I'm having trouble when authenticating a user with it's Microsoft Account. I'm using OpenId Connect Authentication, but when I call the AcquireTokenByAuthorizationCodeAsync method, i get the following message.</p>
  
  <blockquote>
    <p>Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException' in System.Private.CoreLib.dll: 'AADSTS70000: Transmission data parser failure: Authorization Code is malformed or invalid.</p>
  </blockquote>
  
  <p>The auth options are the following ones:</p>
  
  <pre><code>services.AddAuthentication(options =&gt;
          {
              options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
              options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
  
          }).AddOpenIdConnect(openIdOptions =&gt; 
          {
              openIdOptions.ResponseType = OpenIdConnectResponseType.CodeIdToken;
              openIdOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
              openIdOptions.Authority = String.Format(CultureInfo.InvariantCulture, "https://login.microsoftonline.com/{0}{1}", "common", "/v2.0");
              openIdOptions.ClientId = Configuration["MicrosoftAuth:ClientId"];
              openIdOptions.ClientSecret = Configuration["MicrosoftAuth:ClientSecret"];
              openIdOptions.SaveTokens = true;
              openIdOptions.TokenValidationParameters = new TokenValidationParameters{
                  ValidateIssuer = false
              };
              var scopes = Configuration["MicrosoftAuth:Scopes"].Split(' ');
                  foreach (string scope in scopes){
                      openIdOptions.Scope.Add(scope);
              }
              openIdOptions.Events = new OpenIdConnectEvents{
                  OnAuthorizationCodeReceived = async (context) =&gt;
                  {   
                      var code = context.ProtocolMessage.Code;
                      var identifier = context.Principal.Claims.First(item =&gt; item.Type == ObjectIdentifierType).Value;
                      IMemoryCache memoryCache = context.HttpContext.RequestServices.GetRequiredService&lt;IMemoryCache&gt;();
                      var result = await GetTokenByAuthorizationCodeAsync(identifier, code, memoryCache);
                      context.HandleCodeRedemption(result.AccessToken, result.IdToken);
                  },
              };
          });
  </code></pre>
  
  <p>And this is how my GetTokenByAuthorizationCodeAsync looks like this (I know it's not pretty, just trying to make it work):</p>
  
  <pre><code>public async Task&lt;AuthenticationResult&gt; GetTokenByAuthorizationCodeAsync(string userId, string code, IMemoryCache memoryCache)
      {
          TokenCache userTokenCache = new SessionTokenCache(userId, memoryCache).GetCacheInstance();
          try
          {
              AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/common/oauth2/v2.0/token/", userTokenCache);
              ClientCredential credential = new ClientCredential(Configuration["MicrosoftAuth:ClientId"], Configuration["MicrosoftAuth:ClientSecret"]);
              string[] scope = new List&lt;String&gt;().Append("https://graph.windows.net").ToArray();
              AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(code,new Uri(Configuration["MicrosoftAuth:RedirectUri"]), credential, Configuration["MicrosoftAuth:ResourceId"]);
              return result;
          }
          catch (Exception)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>I have no idea of what could be causing the error. I can use the authorization code if i send a postman request, but I'm still not able to add the resource id in that request.</p>
  
  <p>I've registered the app in Microsoft's Application Registration Portal (<a href="https://apps.dev.microsoft.com/" rel="nofollow noreferrer">https://apps.dev.microsoft.com/</a>).</p>