title: Firebase 3: creating a custom authentication token using .net and c#
body: <p>I'm trying to implement Firebase 3 Authentication mechanism using Custom Tokens (as described at https:// firebase.google.com/docs/auth/server/create-custom-tokens).</p>
  
  <p>My server is ASP.NET MVC Application.</p>
  
  <p>So according to the instructions (<a href="https://firebase.google.com/docs/server/setup" rel="noreferrer">https://firebase.google.com/docs/server/setup</a>) I've created a service account for my Firebase application and generated a key in '.p12' format.</p>
  
  <p>After that according to instructions here (<a href="https://firebase.google.com/docs/auth/server/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library" rel="noreferrer">https://firebase.google.com/docs/auth/server/create-custom-tokens#create_custom_tokens_using_a_third-party_jwt_library</a>) I tried to generate  a custom token and sign it using the key received on the previous step. For token generation I used SystemIdentityModel.Tokens.Jwt library from Microsoft, so the code looks like the following:</p>
  
  <pre><code>var now = DateTime.UtcNow;
  var tokenHandler = new JwtSecurityTokenHandler();
  var key = new X509AsymmetricSecurityKey(new X509Certificate2(p12path, p12pwd));
  var signinCredentials = new SigningCredentials(key, "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", "http://www.w3.org/2001/04/xmlenc#rsa-sha256");
  Int32 nowInUnixTimestamp = (Int32)(now.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
  
  var token = tokenHandler.CreateToken(
              issuer: serviceAccountEmail,
              audience: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",                
              signingCredentials: signinCredentials,
              subject: new ClaimsIdentity(new Claim[]
                      {
                      new Claim("sub", serviceAccountEmail),
                      new Claim("iat", nowInUnixTimestamp.ToString()),
                      new Claim("exp", (nowInUnixTimestamp + (60*60)).ToString()),
                      new Claim("uid", uid)
                      })
              );
  
  var tokenString = tokenHandler.WriteToken(token);
  </code></pre>
  
  <p>Then tried to sign in user in React Native application using Firebase Javascript SDK, with the following code:</p>
  
  <pre><code>//omitting initialization code
  firebase.auth().signInWithCustomToken(firebaseJWT).catch(function(error) {
              console.log('Error authenticating Firebase user. Code: ' + error.code + ' Message: ' + error.message);            
          });
  </code></pre>
  
  <p>But got an error from Firebase saying:</p>
  
  <blockquote>
    <p>Error authenticating Firebase user. Code: auth/invalid-custom-token Message: The custom token format is incorrect. Please check the documentation.</p>
  </blockquote>
  
  <p>Experimenting with adding different claims for token expiration control didn't help either.</p>
  
  <p>Also I tried to generate tokens with "dvsekhvalnov/jose-jwt" library but can't get it working with "RS256" algorithm.</p>
  
  <p>So the question:</p>
  
  <p><strong>Any suggestion on what am I doing wrong?</strong></p>