title: Azure AD B2C get token programatically for unit testing
body: <p>My scenario is simple I have a simple Azure Function with B2C authentication on it and I'm writing unit tests but I found an issue, I'm not able to authenticate to the azure functions programmatically. </p>
  
  <p>I'm able to access through the browser and even I can grab the token and put it into the unit test and it works fine, but when I try to generate a token using the ClientID, TenantID, etc. I get a token, but 401 Unauthorized response on the Azure functions.</p>
  
  <p><strong>Is there a way to generate a valid B2C token programmatically (without login in the browser?</strong></p>
  
  <p>The approach I'm using so far:</p>
  
  <pre><code>public static async  Task&lt;AuthenticationResult&gt; GetAccessToken(string resourceUri, string clientId, string clientSecret)
  {
          ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
  
          string aadInstance = "https://login.microsoftonline.com/";
          string tenant = "&lt;mytenant&gt;.onmicrosoft.com";
          string authority = string.Concat(aadInstance, tenant);
          AuthenticationContext authContext = new AuthenticationContext(authority);
  
          return await authContext.AcquireTokenAsync(resourceUri, clientCredential);
  }
  </code></pre>
  
  <p>I'm getting a token (EY.......) but is not valid, when I passed to the Azure Function request, it returns 401 Unauthorized. </p>
  
  <p>Thanks in advance!
  Ivan</p>