Skip to content

Instantly share code, notes, and snippets.

@shibayan
Last active July 25, 2024 02:56
Show Gist options
  • Save shibayan/79d59e7e54c2993b89d0c37075072800 to your computer and use it in GitHub Desktop.
Save shibayan/79d59e7e54c2993b89d0c37075072800 to your computer and use it in GitHub Desktop.
Generate "Sign in with Apple" client_secret using .NET Core (C#)
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using Microsoft.IdentityModel.Tokens;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
const string aud = "https://appleid.apple.com";
const string kid = "0000000000"; // Key ID
const string iss = "0000000000"; // Team ID
const string sub = "ServiceId"; // Client ID (Service ID)
const string privateKey = ""; // Private Key (Base64 Encode)
var ecdsa = ECDsa.Create();
ecdsa.ImportPkcs8PrivateKey(Convert.FromBase64String(privateKey), out _);
var securityKey = new ECDsaSecurityKey(ecdsa)
{
KeyId = kid
};
var descriptor = new SecurityTokenDescriptor
{
Issuer = iss,
Audience = aud,
Subject = new ClaimsIdentity(new[] { new Claim("sub", sub) }),
NotBefore = DateTime.UtcNow,
Expires = DateTime.UtcNow.AddDays(180),
SigningCredentials = new SigningCredentials(securityKey, SecurityAlgorithms.EcdsaSha256)
};
var handler = new JwtSecurityTokenHandler();
var token = handler.CreateJwtSecurityToken(descriptor);
Console.WriteLine(handler.WriteToken(token));
}
}
}
@mehmetutkuk
Copy link

This solution only for windows platform because of the "Windows CNG". I'm looking for cross platform way.

@venujdv
Copy link

venujdv commented Jun 24, 2024

@mehmetutkuk I have same requirement. Did you find any cross platform code? If yes can you please share? Appreciate your help!! Thanks in advance!!!

@shibayan
Copy link
Author

@venujdv Updated. Try it.

@venujdv
Copy link

venujdv commented Jul 16, 2024

Thank you @shibayan . It works if we create from local. If we publish into Azure Function App, it is throwing error like "400-Bad Request" "The system cannot find the file specified." Any idea? Thanks!

@OleksandrOsipchuk
Copy link

@venujdv did you figured out what is the problem? I have the same one and not sure what i can do

@venujdv
Copy link

venujdv commented Jul 25, 2024

@OleksandrOsipchuk I have added "WEBSITE_LOAD_CERTIFICATES" in Azure function app configuration with dummy value. It worked. Please see below link and update here if it works. So that may it will help others in feature. Thanks!

https://stackoverflow.com/questions/46114264/x509certificate2-on-azure-app-services-azure-websites-since-mid-2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment